我有一个Sql查询,我需要将一些条件传递给datetime字段来检索值。例如:我需要客户交易,直到2013年12月31日。但有一个条件是,如果该客户在2014年有任何一笔交易,直到日期也不应该显示。那么如何为此写出where条件?
select distinct T1.CardCode as Customer , T1.CardName as CustomerName
from OINV T3 inner join
OCRD T1
on T1.CardCode = T3.CardCode inner join
OCPR T2
on t1.CardCode = t2.CardCode
where E_MailL = '' and T1.CardType = 'C' and
ValidFor = 'Y' and T3.DocDate = ?
答案 0 :(得分:0)
我怀疑您希望持续日期的客户是2013-12-31并且之后没有交易。您可以使用聚合和HAVING
子句处理此问题:
select T1.CardCode as Customer, T1.CardName as CustomerName
from OINV T3 inner join
OCRD T1
on T1.CardCode = T3.CardCode inner join
OCPR T2
on t1.CardCode = t2.CardCode
where E_MailL = '' and T1.CardType = 'C' and
ValidFor = 'Y'
group by T1.CardCode as Customer, T1.CardName
having max(t3.DocDate) <= ?;
在任何情况下,同样的基本思想也可以基本用于其他日期限制。