I have a table 'FinancialTrans' where only 3 of those fields are needed.
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 only need account numbers where there are No Tolls AND No Pymt in the last 2 years.
My attempt at the code:
I know I need a Having clause but not quite sure how to write it.
Perhaps, a NOT Exist?
SELECT [AcctID]
,[TransTypeCode]
,[TransDate]
FROM [FinancialTrans]
WHERE (
(TransTypeCode = 'TOLL' AND Max(TransDate) <= DATEADD(year, -2, GETDATE()))
OR (TransTypeCode = 'PYMT' AND Max(TransDate) <= DATEADD(year, -2, GETDATE()))
)
GROUP BY AcctID, TransTypeCode, TransDate
The challenge I'm facing is that I want account numbers where there is NEITHER a toll NOR a payment in the past two years. I'm getting account numbers that have no tolls in the past two years but has a payment in the past two years.
Question: How do I ensure I get account numbers that doesn't have BOTH in the past two years? This question is different from an earlier question asked because the requirements have now changed.