1

I'm not the best with datediffs and sql date's. I have a table with 3 columns. Account No, Payment Date, Payment Amount. What i need to do is return only records that have a payment for July but didnt make on for August.

4

2 回答 2

2

几种可能性

SELECT [Account No]
FROM YourTable
WHERE [Payment Date] >= '20130701' AND [Payment Date] < '20130801'
EXCEPT
SELECT [Account No]
FROM YourTable
WHERE [Payment Date] >= '20130801' AND [Payment Date] < '20130901'

或者

SELECT [Account No]
FROM YourTable
WHERE [Payment Date] >= '20130701' AND [Payment Date] < '20130901'
GROUP BY [Account No]
HAVING MAX([Payment Date]) < '20130801'
于 2013-09-11T14:06:33.990 回答
0

像这样的东西:

SELECT * FROM T as T1 
WHERE  YEAR([Payment Date])=2013 
       and MONTH([Payment Date])=7
       and NOT EXISTS(SELECT * FROM T 
                               WHERE T.[Account No]=T1.[Account No]
                                     and YEAR([Payment Date])=2013 
                                     and MONTH([Payment Date])=8
                      )
于 2013-09-11T14:05:47.573 回答