与日期比较?

时间:2009-10-07 06:24:33

标签: sql-server

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

所以......,

从上面的两个表格中,我把Intime的最小值(时间)作为银泰的日期和最长(时间)作为同一日期的停机时间,从table2到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

在我的查询中,Table1为tmp_cardevent1,Table2为Derivedtbl。

从上面我想比较derivedtbl date和tmp_cardevent1 dateIn和DateOut值。

需要查询帮助。

如何查询此情况?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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