如何在两个不同日期之间获得日子(星期一,星期二......)?

时间:2011-12-08 10:58:25

标签: sql vb.net

我需要显示一份报告,该报告将显示周日至星期六的每周培训课程报告,每天的参与人数。 我想要SQL查询来完成此任务。 例如

code    days   mon  tues Wed thurs fri sat sun
10001   3      22   22  22   0     0   0   0
10002   5      10   10  10   10    10  0   0  and so on....

这里没有。参与者在该培训课程的所有日子都是相同的。

更新了查询

.....................我的查询根据@competent_tech建议................... ......

CREATE VIEW SessionDOW
AS
SELECT tcapp_id,title,
case when programtype=1 then 'YP' else 'SP' end programtype,
case when NonResidential=2 then 'Res' else 'Non Res.' end nonresidential,duration, 
fromdate,TODATE,
Training_Calendar.Participents, 
         StartDOW = DATEPART(weekday, FromDate) ,
         EndDOW = DATEPART(weekday, FromDate)+Duration -1,

           weekenddate=DATEPART(d,CONVERT(date,'12-12-2011'))+6
    FROM Training_Calendar

GO

select * from sessiondow

CREATE VIEW SessionDays
AS
  SELECT SessionDOW.*,
         MON = CASE WHEN (2 BETWEEN StartDOW AND EndDOW)  OR (EndDow >= 2 + 7) THEN Participents ELSE 0 END ,
         TUE = CASE WHEN (3 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 3 + 7) THEN Participents ELSE 0 END ,
         WED = CASE WHEN (4 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 4 + 7) THEN Participents ELSE 0 END ,
         THURS = CASE WHEN (5 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 5 + 7) THEN Participents ELSE 0 END ,
         FRI = CASE WHEN (6 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 6 + 7) THEN Participents ELSE 0 END ,
         SAT = CASE WHEN (7 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 0 + 7) THEN Participents ELSE 0 END,
           SUN = CASE WHEN (1 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 1 + 7) THEN Participents  ELSE 0 END 
    FROM SessionDOW
GO


SELECT e.* 
FROM SessionDays e INNER JOIN Training_Calendar a ON e.tcapp_id = a.TCApp_Id
GROUP BY e.TCApp_Id,e.title,
e.programtype,
e.nonresidential,e.duration, 
e.Participents, 
e.StartDOW ,
e.EndDOW,sun,MON,tue,wed,thurs,fri,SAT,e.FromDate,e.todate,e.weekenddate

给我以下结果

tcapp_id    title   programtype nonresidential  duration    fromdate    TODATE  Participents    StartDOW    EndDOW  weekenddate MON TUE WED THURS   FRI SAT SUN
2011-12/CHD-10001   testing SP  Res 10  2011-12-05  2011-12-14  60  2   11  18  60  60  60  60  60  60  60
2011-12/CIT-10001   Training of Palak Technical Officer YP  Res 2   2011-12-13  2011-12-14  45  3   4   18  0   45  45  0   0   0   0
2011-12/SIRD-10001  Induction Level Training Programme for JE's of MESCT    YP  Res 4   2011-12-12  2011-12-15  25  2   5   18  25  25  25  25  0   0   0
2011-12/SIUD-10001  Extempore Leadership Skills Workshop    SP  Non Res.    5   2011-12-14  2011-12-18  35  4   8   18  0   0   35  35  35  35  35
2011-12/SIUD-10002  fdgd    YP  Res 6   2011-12-15  2011-12-20  80  5   10  18  80  80  0   80  80  80  80

这周从12月12日开始到结束于18月12日,但培训课程从第5个月开始,到第14个结束,但接下来的几天(15,16,17,18)显示参与者的价值。请帮忙。

1 个答案:

答案 0 :(得分:0)

如果这是一个SQL Server数据库,那么您的查询将是:

SELECT CODE ,
       SUM(1) AS DAYS ,
       SUN = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 0 THEN 1 ELSE 0 END) ,
       MON = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 1 THEN 1 ELSE 0 END) ,
       TUE = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 2 THEN 1 ELSE 0 END) ,
       WED = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 3 THEN 1 ELSE 0 END) ,
       THURS = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 4 THEN 1 ELSE 0 END) ,
       FRI = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 5 THEN 1 ELSE 0 END) ,
       SAT = SUM(CASE WHEN DATEPART(weekday, TimeStamp) = 6 THEN 1 ELSE 0 END)
  FROM Event
GROUP BY code

其中Timestamp是包含培训日期的列的名称,Event是表格的名称。

<强>更新

上述查询假设每个学生每天参加课程时,数据库中都有一条记录。

但是,如果数据库设计是这样的,那么会话的一条记录指定了开始日期和天数,而另一个表是为参与者注册给定会话的,则查询会有所不同。 / p>

假设以下粗表结构:

CREATE TABLE Sessions
(
   code INT NOT NULL IDENTITY(1, 1) ,
   date DATETIME NOT NULL ,
   DAYS INT NOT NULL ,
   PRIMARY KEY (code)
)
CREATE TABLE Attendees
(
   id INT NOT NULL IDENTITY(1, 1) ,
   code INT NOT NULL ,
   NAME NVARCHAR(100)
   PRIMARY KEY (id) ,
   FOREIGN KEY (code) REFERENCES sessions (code)
)

我们首先创建一个视图来计算一周的开始和结束日期(这使得后续视图更容易理解):

CREATE VIEW SessionDOW
AS
  SELECT CODE ,
         DAYS ,
         StartDOW = DATEPART(weekday, date) ,
         EndDOW = DATEPART(weekday, date) + DAYS 
    FROM Sessions
GO

然后,我们创建一个视图,以便为会话运行的每周中的每一天记录1。请注意,此视图会考虑跨越两周的会话:

CREATE VIEW SessionDays
AS
  SELECT CODE ,
         DAYS ,
         SUN = CASE WHEN (0 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 0 + 7) THEN 1 ELSE 0 END ,
         MON = CASE WHEN (1 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 1 + 7) THEN 1 ELSE 0 END ,
         TUE = CASE WHEN (2 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 2 + 7) THEN 1 ELSE 0 END ,
         WED = CASE WHEN (3 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 3 + 7) THEN 1 ELSE 0 END ,
         THURS = CASE WHEN (4 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 4 + 7) THEN 1 ELSE 0 END ,
         FRI = CASE WHEN (5 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 5 + 7) THEN 1 ELSE 0 END ,
         SAT = CASE WHEN (6 BETWEEN StartDOW AND EndDOW) OR (EndDow >= 6 + 7) THEN 1 ELSE 0 END
    FROM SessionDOW
GO

最后,我们将参加每个会话的学生加入会话日计数并添加值:

  SELECT e.CODE ,
         MAX(DAYS) AS DAYS ,
         SUN = SUM(SUN) ,
         MON = SUM(MON) ,
         TUE = SUM(TUE) ,
         WED = SUM(WED) ,
         THURS = SUM(THURS) ,
         FRI = SUM(FRI) ,
         SAT = SUM(SAT)
    FROM SessionDays e INNER JOIN Attendees a ON e.code = a.code
GROUP BY e.code