SQL两个日期之间的时间差多个条件

huangapple go评论69阅读模式
英文:

SQL time difference between two dates multiple conditions

问题

ID 日期 日期2 N天 事件
1 01/01/2010 06/01/2010 5 A
1 06/01/2010 09/01/2010 3 A
1 04/01/2010 15/01/2010 11 B
2 01/02/2010 04/04/2010 62 A
2 01/02/2010 04/04/2010 62 C

英文:

I have a table (T1) which looks like that :

    ID	Date	Event
1	01/01/2010	A
1	04/01/2010	B
1	06/01/2010	A
1	09/01/2010	A
1	15/01/2010	B
2	01/02/2010	A
2	04/04/2010	A
2	01/02/2010	C
2	04/04/2010	C

I would like to calculate the difference between two dates based on "ID" and "Event" Columns. I would need a table (T2) which looks like that :

ID	Date	Date2	Ndays	Event
1	01/01/2010	06/01/2010	5	A
1	06/01/2010	09/01/2010	3	A
1	04/01/2010	15/01/2010	11	B
2	01/02/2010	04/04/2010	62	A
2	01/02/2010	04/04/2010	62	C

答案1

得分: 2

以下是翻译好的部分:

一个简单的Group By查询足以:

SQL查询如下:

SELECT 
    Events.ID, 
    Min(Events.Date) AS Date1, 
    Max(Events.Date) AS Date2, 
    DateDiff('d',[Date1],[Date2]) AS NDays, 
    Events.Event
FROM 
    Events
GROUP BY 
    Events.ID, 
    Events.Event;

输出:

SQL两个日期之间的时间差多个条件

编辑:

对于更多记录,使用子查询:

SQL查询如下:

SELECT 
    Events.ID, 
    Events.Date AS Date1, 
    (SELECT Min(T.Date) 
    FROM Events AS T 
    WHERE T.ID = Events.ID AND T.Event = Events.Event AND T.Date > Events.Date) AS Date2, DateDiff('d',[Date1],[Date2]) AS NDays, 
    Events.Event
FROM 
    Events
GROUP BY 
    Events.ID, 
    Events.Date, 
    Events.Event
HAVING 
    (SELECT Min(T.Date) 
    FROM Events AS T 
    WHERE T.ID = Events.ID AND T.Event = Events.Event AND T.Date > Events.Date) IS NOT NULL
ORDER BY 
    Events.ID, 
    Events.Event, 
    Events.Date;

输出:

SQL两个日期之间的时间差多个条件

英文:

A simple Group By query will do:

SELECT 
    Events.ID, 
    Min(Events.Date) AS Date1, 
    Max(Events.Date) AS Date2, 
    DateDiff('d',[Date1],[date2]) AS NDays, 
    Events.Event
FROM 
    Events
GROUP BY 
    Events.ID, 
    Events.Event;

Output:

SQL两个日期之间的时间差多个条件

Edit:

For more records, use a subquery:

SELECT 
    Events.ID, 
    Events.Date AS Date1, 
    (Select Min(T.Date) 
    From Events As T 
    Where T.ID = Events.ID And T.Event = Events.Event And T.Date > Events.Date) AS Date2, DateDiff('d',[Date1],[date2]) AS NDays, 
    Events.Event
FROM 
    Events
GROUP BY 
    Events.ID, 
    Events.Date, 
    Events.Event
HAVING 
    (Select Min(T.Date) 
    From Events As T 
    Where T.ID = Events.ID And T.Event = Events.Event And T.Date > Events.Date) Is Not Null
ORDER BY 
    Events.ID, 
    Events.Event, 
    Events.Date;

Output:

SQL两个日期之间的时间差多个条件

答案2

得分: 0

使用自连接和聚合:

SELECT t.ID, t.Date,
  MIN(tt.Date) AS Date2,
  DATEDIFF('d', t.Date, Date2) AS NDays,
  t.Event
FROM tablename t INNER JOIN tablename tt
ON tt.ID = t.ID AND tt.Event = t.Event AND t.Date < tt.Date
GROUP BY t.ID, t.Date, t.Event
ORDER BY t.ID, t.Event, t.Date

结果:

ID      Date        Date2       NDays   Event
1       1/1/2010    6/1/2010    5       A
1       6/1/2010    9/1/2010    3       A
1       4/1/2010    15/1/2010   11      B
2       1/2/2010    4/4/2010    62      A
2       1/2/2010    4/4/2010    62      C
英文:

With a self join and aggregation:

SELECT t.ID, t.Date, 
  MIN(tt.Date) AS Date2, 
  DATEDIFF(&#39;d&#39;, t.Date, Date2) AS NDays,
  t.Event
FROM tablename t INNER JOIN tablename tt
ON tt.ID = t.ID AND tt.Event = t.Event AND t.Date &lt; tt.Date
GROUP BY t.ID, t.Date, t.Event
ORDER BY t.ID, t.Event, t.Date

Results:

ID	Date	    Date2	    NDays	Event
1	1/1/2010	6/1/2010	5	    A
1	6/1/2010	9/1/2010	3	    A
1	4/1/2010	15/1/2010	11	    B
2	1/2/2010	4/4/2010	62	    A
2	1/2/2010	4/4/2010	62	    C

huangapple
  • 本文由 发表于 2020年1月3日 16:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定