【问题标题】:comparing with dates?与日期比较?
【发布时间】:2009-10-07 06:24:33
【问题描述】:

使用 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 作为日期的 Intime,并将 max(时间)作为同一日期的 Outtime,来自 table2 where 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 日期与 tmp_cardevent1 dateIn 和 DateOut 值。

需要查询帮助。

如何查询这个条件?

【问题讨论】:

    标签: sql-server


    【解决方案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
    

    【讨论】:

    • 我很困惑,我发布了我的实际查询,您可以检查一下
    • 我向您展示了一种尝试这样做的方法。看看你是否明白我的意思,如果你不明白就问问题
    • 我在这里感到困惑。你能解释一下吗?从@Table1 t1 左连接@Table2 t2IN ON t1.ID = t2IN.ID AND t1.DateIn = t2IN.Date 左连接@Table2 t2Out ON t1.ID = t2Out.ID AND t1.DateOut = t2Out.Date
    • 我使用 DateIn 将 table1 中的条目(行)连接到 table2(别名 t2In)以获取连接时的 smallets 时间,连接 table2(别名 t2Out)也是如此,以获得最大时间。
    【解决方案2】:

    我认为这样做可以:

    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
    

    【讨论】:

    • 什么是更新,不要求更新,我需要选择与日期比较的值。
    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 2011-04-10
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多