Case with multiple conditions in SQL Server 2014

2019-03-05 14:06发布

问题:

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