简体   繁体   English

每天每小时进行一次SQL查询

[英]SQL query hourly for each day

I have a question that seems to be quite complex. 我有一个似乎很复杂的问题。 I needed to know what happens in a session that day, at a certain time. 我需要知道当天某个时间段的某个会话中发生了什么。

Briefly I have a table that shows me all sessions of a given area. 简要地说,我有一张表,显示给定区域的所有会话。 These sessions have a start date and a start time and an end time. 这些会话具有开始日期,开始时间和结束时间。

You can see in this table: 您可以在此表中看到:

idArea | idSession |  startDate  |    startTime        |    endTime  
   1   |    1      |  2013-01-01 | 1900-01-01 09:00:00 | 1900-01-01 12:00:00
   1   |    2      |  2013-01-01 | 1900-01-01 14:00:00 | 1900-01-01 15:00:00
   1   |    3      |  2013-01-04 | 1900-01-01 09:00:00 | 1900-01-01 13:00:00
   1   |    4      |  2013-01-07 | 1900-01-01 10:00:00 | 1900-01-01 12:00:00
   1   |    5      |  2013-01-07 | 1900-01-01 13:00:00 | 1900-01-01 18:00:00
   1   |    6      |  2013-01-08 | 1900-01-01 10:00:00 | 1900-01-01 12:00:00

Then I also have a table that shows me all hours interspersed, ie every half hour (I created this table on purpose for this requirement, if someone has a better idea, I can say that I will try to adapt). 然后,我还有一个表格,显示所有时间,即每半小时(我为此目的专门创建了该表格,如果有人有更好的主意,我可以说我会尝试适应)。

idHour |   Hour
   1   |   1900-01-01 00:00:00
   2   |   1900-01-01 00:30:00
   3   |   1900-01-01 01:00:00
  ............................
   4   |   1900-01-01 09:00:00
   5   |   1900-01-01 09:30:00
   6   |   1900-01-01 10:00:00
   7   |   1900-01-01 10:30:00
  ............................

In the end that's what I want to present was this: 最后,我要介绍的是:

  startDate  |    startTime        |    SessionID
  2013-01-01 | 1900-01-01 09:00:00 |        1
  2013-01-01 | 1900-01-01 09:30:00 |        1
  2013-01-01 | 1900-01-01 10:00:00 |        1
  2013-01-01 | 1900-01-01 10:30:00 |        1
  2013-01-01 | 1900-01-01 11:00:00 |        1
  2013-01-01 | 1900-01-01 11:30:00 |        1
  2013-01-01 | 1900-01-01 11:30:00 |        1
  2013-01-01 | 1900-01-01 14:00:00 |        1
  2013-01-01 | 1900-01-01 14:30:00 |        1
  2013-01-01 | 1900-01-01 15:00:00 |        1

This table is only for idSession=1 what I wanted was for all sessions. 该表仅适用于idSession = 1我想要的所有会话。 If there are no sessions for one day can return NULL. 如果一天没有任何会话,则可以返回NULL。

The hard this query or procedure, is that they have to show me all the days of the month when there are sessions for that area. 这个查询或过程的难点是,当该区域有会议时,他们必须在每个月的所有时间告诉我。

For this, I already used this query: 为此,我已经使用了以下查询:

;WITH t1 AS 
(
    SELECT  
        startDate,
        DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', startDate), '1900-01-01') firstInMonth,
        DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', startDate) + 1, '1900-01-01')) lastInMonth,
        COUNT(*) cnt
    FROM    
        @SessionsPerArea
    WHERE
        idArea = 1
    GROUP BY
        startDate
), calendar AS
(
    SELECT DISTINCT 
        DATEADD(DAY, c.number, t1.firstInMonth) d
    FROM    
        t1 
    JOIN
        master..spt_values c ON type = 'P'
                             AND DATEADD(DAY, c.number, t1.firstInMonth) BETWEEN t1.firstInMonth AND t1.lastInMonth
)
SELECT  
    d date, 
    cnt Session
FROM
    calendar c
LEFT JOIN 
    t1 ON t1.startDate = c.d

It is quite complex, if anyone has an easy way to do this was excellent. 如果有人能轻松做到这一点,那就太好了。

If I understand correctly, this is simply a join between the calendar table and @SessionPerArea,w ith the right conditions: 如果我理解正确,那么这只是日历表和@SessionPerArea之间的联接,并具有正确的条件:

select spa.StartDate, c.hour as StartTime, spa.idSession as SessionId
from calendar c join
     @SessionsPerArea spa
     on c.hour between spa.startTime and spa.EndTime

The join is matching all times between the start and end times in the data, and then returning the values. 联接将匹配数据中开始时间和结束时间之间的所有时间,然后返回值。

我认为您可能只是需要在Calendar和@SessionsPerArea之间进行外部联接...因此,无论与@SessionsPerArea表是否匹配,都将返回日历表中的所有日期?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM