我有一张这样的表:
CustID VisitTime
1 2012-12-31 11:59
1 2013-1-1 00:00
1 2013-1-1 00:05
2 2013-1-1 00:20
2 2013-1-1 10:00
3 2013-1-1 11:40
现在,我想选择那些访问过Jan 1st 12:00AM
到Jan 31st 11:59pm
网站的新客户。
例如:cust ID 1
也访问了Dec
,因此不应选择cust ID 1
。只应选择2
和3
。
如何使用t-sql
?
答案 0 :(得分:5)
SELECt t.CustID
FROM Table1 t
GROUP BY t.CustID
HAVING MIN(VisitTime) >='01/01/2013 00:00:00' AND MIN(VisitTime) < '02/01/2013 00:00:00'
--same test on MAX(vistTime) if needed
答案 1 :(得分:0)
SELECT DISTINCT CUSTID
FROM T as T1
WHERE VisitTime between '2013-01-01 00:00' AND '2013-01-31 23:59'
AND NOT EXISTS (SELECT T.CustID FROM T
WHERE T.CustId=T1.CustId and VisitTime < '2013-01-01')