Case with multiple conditions in SQL Server 2014

一个人想着一个人 提交于 2020-01-11 13:13:50

问题


I have a table 'FinancialTrans' which has many fields but only 3 of those fields concern me.

AcctID  TransTypeCode   DateOfTrans  Field 4  Field 5  Field 6....
123     TOLL            2016-06-06
123     TOLL            2016-06-02
123     TOLL            2016-04-28
123     PYMT            2016-03-11
123     TOLL            2015-12-22
123     TOLL            2015-12-22

What I need:
I need to print a Flag when there are no TOLL or PYMT in the last 2 years.

So in plain logic:

(When TransTypeCode is 'TOLL' and the MAX(DateOfTrans) is more than 2 years ago)  AND  
(When TransTypeCode is 'PYMT' and the MAX(DateOfTrans) is more than 2 years ago)

So my code so far is:

select *,
(case when max(case when FT.TransTypeCode in ('TOLL', 'PYMT') 
                    then FT.DateOfTrans 
                    end) 
      over (partition by FT.Acctid) >= dateadd(year, -2, getdate())
      then 0 else 1
      end) as MyFlag

from #temp_TableA A
INNER JOIN FinancialTrans FT ON A.AccountId = FT.AcctId

But this code brings along all the other fields of the FinancialTrans table and joins each row for that account number with previous table. Hence, I get about 1200 duplicates for each account number.

Question:
1. How do I NOT get 1200 duplicates for each account number?
2. How do I get a column which gives a flag when the above two conditions are met.


回答1:


Use GROUP BY, so you get a single row for account, instead of row_number where you calculate something for every row.

SELECT A.AcctID,
       MAX(case when FT.TransTypeCode in ('TOLL', 'PYMT') 
                 and FT.DateOfTrans >= dateadd(year, -2, getdate())
                then 1
                else 0
           end) as flag

FROM #temp_TableA A
INNER JOIN FinancialTrans FT ON A.AccountId = FT.AcctId
GROUP BY A.AcctID


来源:https://stackoverflow.com/questions/47313625/case-with-multiple-conditions-in-sql-server-2014

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!