2009-10-07 101 views
0

使用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值。

需要查詢幫助。

如何查詢這種情況?

回答

1

嘗試是這樣的

DECLARE @Table1 TABLE(
     ID VARCHAR(50), 
     PNAME VARCHAR(50), 
     Date DATETIME, 
     TimeIN VARCHAR(10), 
     [TimeOut] VARCHAR(10), 
     DateIn DATETIME, 
     DateOut DATETIME 
) 

INSERT INTO @Table1 (ID,PNAME,Date,TimeIN,[TimeOut],DateIn,DateOut) 
SELECT 'AEAA00294','Alexander','13 Oct 2008', '09:00:00', '18:00:00','13 Oct 2008','13 Oct 2008' 
INSERT INTO @Table1 (ID,PNAME,Date,TimeIN,[TimeOut],DateIn,DateOut) 
SELECT 'AEAA00294','Alexander','14 Oct 2008', '16:00:00', '02:00:00','14 Oct 2008','15 Oct 2008' 
INSERT INTO @Table1 (ID,PNAME,Date,TimeIN,[TimeOut],DateIn,DateOut) 
SELECT 'AEAA00294','Alexander','16 Oct 2008', '09:00:00', '18:00:00','16 Oct 2008','16 Oct 2008' 

DECLARE @Table2 TABLE(
     Date DATETIME, 
     [Time] VARCHAR(10), 
     ID VARCHAR(50) 
) 

INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008', '10:34:17', 'AEAA00294' 
INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008' , '15:15:52' , 'AEAA00294' 
INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008' , '17:08:36' , 'AEAA00294' 
INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008' , '17:09:09' , 'AEAA00294' 
INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008' , '17:10:15' , 'AEAA00294' 
INSERT INTO @Table2 (Date,[Time],ID) SELECT '14 Oct 2008' , '16:36:48' , 'AEAA00294' 
INSERT INTO @Table2 (Date,[Time],ID) SELECT '14 Oct 2008' , '03:08:38' , 'AEAA00294' 
INSERT INTO @Table2 (Date,[Time],ID) SELECT '15 Oct 2008' , '14:47:08' , 'AEAA00294' 
INSERT INTO @Table2 (Date,[Time],ID) SELECT '15 Oct 2008' , '15:11:33' , 'AEAA00294' 
INSERT INTO @Table2 (Date,[Time],ID) SELECT '16 Oct 2008' , '09:52:11' , 'AEAA00294' 


SELECT t1.ID, 
     t1.PNAME, 
     t1.DateIn, 
     t1.DateOut, 
     MIN(CAST(t2In.[Time] AS DATETIME)), 
     MAX(CAST(t2Out.[Time] AS DATETIME)) 
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 
GROUP BY t1.ID, 
     t1.PNAME, 
     t1.DateIn, 
     t1.DateOut 
+0

我很困惑,我發佈了我的實際查詢,你可以查詢 – Gopal 2009-10-07 07:28:02

+0

我告訴你一種方法來嘗試去做。看看你是否明白我的意思,並提出問題,如果你不 – 2009-10-07 08:40:11

+0

我混亂在這裏。你能解釋這一個嗎? 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

0

我想這會做到這一點:

update table1 set timein=t2.timein, timeout=t2.timeout from table1 t1 join 
(select ID, date, min(time) as timein, max(time) as timeout from table2 group by is, date) as t2 on t1.id=t2.id 
+0

什麼是產品更新,我不要求更新,我需要選擇價值與日期進行比較。 – Gopal 2009-10-07 07:23:15