如何获得约会时间?

时间:2009-10-12 07:44:42

标签: sql-server

表1

ID Date Time Functionkey

001 23-02-2009, 08:00:00 A
001 23-02-2009, 12:00:00 A
001 23-02-2009, 13:00:00 B
001 23-02-2009, 14:00:00 B
001 23-02-2009, 16:00:00 C
001 23-02-2009, 17:00:00 C
001 23-02-2009, 19:00:00 D
001 23-02-2009, 21:00:00 D
001 24-02-2009 10:00:00 A
001 24-02-2009 18:00:00 D
001 25-02-2009 09:00:00 A
001 25-02-2009 12:00:00 B
001 25-02-2009 18:00:00 D
....,

我想得到min(时间),其中function key ='A',max(time)其中function key ='B',min(time)其中function key ='c'。最大(时间),其中功能键='D' 查询:

   SELECT 
      ID, Date, 
      CASE WHEN function = 'A' THEN min(time) END as Intime1,
      CASE WHEN function = 'B' THEN max(time) END as Outtime1, 
      CASE WHEN function = 'C' THEN min(time) END as Intime2,  
      CASE WHEN function = 'D' THEN max(time) END as Outtime2 
   FROM dbo.table1 
   GROUP BY id, date, function

获得输出

ID Date, Intime1, Outtime1, Intime2, Outtime2

001 23-02-2009 08:00:00, null, null, null
001 23-02-2009 null, 14:00:00, null, null
001 23-02-2009 null, null, 16:00:00, null
001 23-02-2009 null, null, null, 21:00:00

...

我需要输出一行,如何进行查询

ID Date, Intime1, Outtime1, Intime2, Outtime2

001 23-02-2009 08:00:00, 14:00:00. 16:00:00, 21:00:00 
001 24-02-2009 09:00:00  null, null, 18:00:00
001 25-02-2009 09:00:00, 12:00:00, null, 18:00:00

...

如何修改我的查询?

3 个答案:

答案 0 :(得分:2)

尝试类似:

SELECT T1.ID, T1.Date, MIN(T1.Time) As Intime, MAX(T2.Time) AS Outtime
FROM Table1 T1
JOIN Table1 T2 ON T1.ID=T2.ID AND T1.Date=T2.Date
WHERE T1.Function = 'A' AND T2.Function='D'
GROUP BY T1.ID, T1.Date

编辑:关于您的评论,您只需要使用最小函数(参见上面的MIN(T1.Time))和表T2的条件更改表T1的条件。函数最大值(参见上面的MAX(T2.Time)),所以应该是:

SELECT T1.ID, T1.Date, MIN(T1.Time) As Intime, MAX(T2.Time) AS Outtime
FROM Table1 T1
JOIN Table1 T2 ON T1.ID=T2.ID AND T1.Date=T2.Date
WHERE T1.Function = 'C' AND T2.Function='D'
GROUP BY T1.ID, T1.Date

我希望我能从评论中正确理解你的需要。

编辑2 :好的,我现在知道了。你需要再做两次这样的自我加入:

SELECT 
    T1.ID, T1.Date, 
    MIN(T1.Time) AS Intime1,
    MAX(T2.Time) AS Outtime1,
    MIN(T3.Time) AS Intime2,
    MAX(T4.Time) AS Outtime2
FROM Table1 T1
JOIN Table1 T2
    ON T1.ID=T2.ID AND T1.Date=T2.Date
JOIN Table1 T3
    ON T1.ID=T3.ID AND T1.Date=T3.Date
JOIN Table1 T4
    ON T1.ID=T4.ID AND T1.Date=T4.Date
WHERE 
    T1.Func = 'A' 
    AND T2.Func='B'
    AND T3.Func='C'
    AND T4.Func='D'
GROUP BY
    T1.ID, T1.Date

编辑3 :要考虑可能的缺失值,请尝试下一个查询。需要存在的唯一值是函数“A”。其余的可能缺席:

SELECT 
    T1.ID, T1.Date, 
    MIN(T1.Time) AS Intime1,
    MAX(T2.Time) AS Outtime1,
    MIN(T3.Time) AS Intime2,
    MAX(T4.Time) AS Outtime2
FROM Table1 T1
LEFT JOIN Table1 T2
    ON T1.ID=T2.ID AND T1.Date=T2.Date AND T2.Func = 'B'
LEFT JOIN Table1 T3
    ON T1.ID=T3.ID AND T1.Date=T3.Date AND T3.Func = 'C'
LEFT JOIN Table1 T4
    ON T1.ID=T4.ID AND T1.Date=T4.Date AND T4.Func = 'D'
WHERE 
    T1.Func = 'A' 
GROUP BY
    T1.ID, T1.Date

答案 1 :(得分:0)

您真正应该考虑的一件事是将日期/时间存储为格式化为unix时间戳的单个列,它使处理日期/时间变得更加容易。

答案 2 :(得分:0)

尝试以下方法:

SELECT  ID, 
        Date, 
        min(time) AS Intime, 
        max(time) AS Outtime 
FROM table1 
GROUP BY id, date