使用SQL Server 2000與日期相比較?
表1
ID Name, Date, TimeIn, TimeOut, DateIn, Dateout
AEAA00294 Alexander 13/10/2008 09:00:00 18:00:00 13/10/2008 13/10/2008
AEAA00294 Alexander 14/10/2008 16:00:00 02:00:00 14/10/2008 15/10/2008
AEAA00294 Alexander 16/10/2008 09:00:00 18:00:00 16/10/2008 16/10/2008
...等,
表2
ID Date
DATE, TIME, ID
20081013 103417 AEAA00294
20081013 151552 AEAA00294
20081013 170836 AEAA00294
20081013 170909 AEAA00294
20081013 171015 AEAA00294
20081014 163648 AEAA00294
20081014 030838 AEAA00294
20081015 144708 AEAA00294
20081015 151133 AEAA00294
20081016 095211 AEAA00294
...等,
從以上兩個表我正銀泰時間是分(時間)銀泰在日期和最大(時間)作爲從table2的同一日期的Outtime,其中table1.personid = table2.personid。
In table1 DateIn and Dateout Date is same, it should take min (time) and Max (time) on the same date from the table2
Suppose table1 DateIn and Dateout Date is different, it should take min (time) and Max (time) from table2 compare with table1.dateIn and table2.Dateout
期望輸出
ID Date Intime Outtime
AEAA00294 20081013 103417 171015
AEAA00294 20081014 030838 151133
...等,
我的查詢
SELECT DISTINCT DERIVEDTBL.PERSONID, DERIVEDTBL.CARDEVENTDATE, MIN(DERIVEDTBL.CARDEVENTTIME) AS INTIME, MAX(DERIVEDTBL.CARDEVENTTIME) AS OUTTIME, tmp_Cardevent1.Normal_Intime, tmp_Cardevent1.Normal_Outtime, tmp_Cardevent1.CardEventDate AS Expr1, tmp_Cardevent1.DateIn, tmp_Cardevent1.DateOut FROM (SELECT T_PERSON.PERSONID, T_CARDEVENT.CARDEVENTDATE, CONVERT(VARCHAR(10), SUBSTRING(T_CARDEVENT.CARDEVENTTIME, 1, 2) + ':' + SUBSTRING(T_CARDEVENT.CARDEVENTTIME, 3, 2) + ':' + SUBSTRING(T_CARDEVENT.CARDEVENTTIME, 5, 2), 8) AS CARDEVENTTIME
FROM T_CARDEVENT LEFT JOIN T_PERSON ON T_CARDEVENT.PERSONID = T_PERSON.PERSONID) DERIVEDTBL INNER JOIN tmp_Cardevent1 ON DERIVEDTBL.PERSONID = tmp_Cardevent1.PERSONID AND DERIVEDTBL.CARDEVENTDATE = tmp_Cardevent1.CardEventDate GROUP BY DERIVEDTBL.CARDEVENTDATE, DERIVEDTBL.PERSONID, tmp_Cardevent1.Normal_Intime, tmp_Cardevent1.Normal_Outtime, tmp_Cardevent1.CardEventDate, tmp_Cardevent1.DateIn, tmp_Cardevent1.DateOut
表1爲我的查詢tmp_cardevent1和表2爲Derivedtbl。
從上面我想比較derivedtbl日期與tmp_cardevent1 dateIn和DateOut值。
需要查詢幫助。
如何查詢這種情況?
我很困惑,我發佈了我的實際查詢,你可以查詢 – Gopal 2009-10-07 07:28:02
我告訴你一種方法來嘗試去做。看看你是否明白我的意思,並提出問題,如果你不 – 2009-10-07 08:40:11
我混亂在這裏。你能解釋這一個嗎? FROM @ Table1 t1 LEFT JOIN @ Table2 t2IN ON t1.ID = t2IN.ID AND t1.DateIn = t2IN.Date LEFT JOIN @ Table2 t2Out ON t1.ID = t2Out.ID AND t1.DateOut = t2Out.Date – Gopal 2009-10-07 08:44:45