倒霉得很,公司的數據庫是2000的,rownumber用不了,WITH AS用不了,查詢條件還慢復雜,想了蠻久,沒啥辦法了,求助下。下面是最簡單的一條sql:
SELECT a.autoid,a.callerid,a.doorid,a.cardday,a.eventtype
FROM Cr_CardEvent a, Cr_Caller_BasicInfo b
WHERE a.callerid = b.callerid AND b.callertype = 1
AND a.cardday BETWEEN '2011-09-01' AND '2011-09-02'
拋磚引玉...
下面這條是上面的小小升級版
SELECT c.autoid,c.callerid,c.cardid,c.doorid,c.cardday,c.eventtype,d.doorid AS max_doorId,d.eventType AS max_eventSort,d.cardday AS max_cardDay
FROM (SELECT * FROM cr_cardevent a WHERE EXISTS (SELECT 1 FROM(SELECT MIN(cardday)min_time ,callerid FROM cr_cardevent c,Com_EmpCard card
WHERE cardday BETWEEN '2014-09-01' AND '2014-10-01' AND c.cardid=card.cardid AND EXISTS
(SELECT 1 FROM Finger_Data fingure where fingure.cardid=card.cardid) GROUP BY convert(varchar(10),cardday,121), callerid)b
WHERE a.cardday=b.min_time AND a.callerid=b.callerid ))c ,(SELECT * FROM cr_cardevent a WHERE EXISTS
(SELECT 1 FROM(SELECT max(cardday)min_time ,callerid
FROM cr_cardevent c,Com_EmpCard card where cardday BETWEEN '2014-09-01' AND '2014-10-01' AND c.cardid=card.cardid GROUP BY convert(varchar(10),cardday,121), callerid)b WHERE a.cardday=b.min_time AND a.callerid=b.callerid))d,cr_caller_basicinfo e
WHERE c.callerid = d.callerid AND c.callerid = e.callerid AND e.callertype=1
AND 0=DATEDIFF(DAY, convert(varchar(10),c.cardday,121),convert(varchar(10),d.cardday,121))
最後用的游標解決問題。在這種rownumber函數和CTE沒法用並且限制頗多(權限)的sqlserver2000,只能用了游標去分頁