我不是最好的日期和sql日期。我有一个有3列的表。 Account No, Payment Date, Payment Amount
。我需要做的只是返回7月份付款但8月没有付款的记录。
答案 0 :(得分: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'
答案 1 :(得分: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
)