将周日期范围显示为SQL

时间:2017-08-23 05:51:20

标签: sql sql-server

我有一个查询来每天获取计数,其中startdate和enddate是从应用程序传递的参数,我在这里使用了一个示例日期来演示查询

SELECT  
    CAST(DateCreated AS DATE) AS Date, 
    COUNT(0) AS Total  
FROM 
    SessionTable
WHERE  
    DateCreated >= '10/02/2015'  
    AND CONVERT(DATE, DateCreated) <= '10/30/2015' 
GROUP BY
    CAST(DateCreated AS DATE) 
ORDER BY
    CAST(DateCreated AS DATE) 

这将以格式

返回结果
Date Total 

现在我决定通过将日期计数转换为按周计数来减少记录数量,并使用查询

SELECT 
    DATEPART(wk, DateCreated) AS WeekNumber, 
    COUNT(0) AS WeekCount 
FROM 
    SessionTable
WHERE  
    DateCreated >= '10/02/2015'  
    AND CONVERT(DATE, DateCreated) <= '10/30/2015' 
GROUP BY 
    DATEPART(wk, DateCreated)

以格式返回结果并且工作正常

WeekNumber  WeekCount
-------------------------
  40           2298
  41          13998
  42          19454
  43          19923

为了使结果集更清晰,我打算将每周的开始日期和结束日期添加到上面的结果集中,但很难这样做

我的预期结果是

    WeekNumber BeginDate  EndDate WeekCount 

要从中获取weekBegin和结束日期How do you get the "week start date" and "week end date" from week number in SQL Server?建议可以使用,我写了像

这样的查询
         SELECT 
    DATEPART(wk, DateCreated) AS WeekNumber, 
    DATEADD(dd, -(DATEPART(dw, DateCreated)-1), DateCreated) as [WeekStart],
    DATEADD(dd, 7-(DATEPART(dw, DateCreated)), DateCreated)  as [WeekEnd],
    COUNT(0) AS WeekCount 
    FROM 
    Session
    WHERE  
    DateCreated >= '10/02/2015'  
    AND CONVERT(DATE, DateCreated) <= '10/30/2015' 
    GROUP BY 
    DATEPART(wk, DateCreated),
    DATEADD(dd, -(DATEPART(dw, DateCreated)-1), DateCreated),
    DATEADD(dd, 7-(DATEPART(dw, DateCreated)), DateCreated)

但这会返回我的结果集,每天的计数为1,如下所示

    WeekNumber  WeekStart       WeekEnd                     WeekCount
    44  2015-10-25 18:02:26.527 2015-10-31 18:02:26.527 1
    44  2015-10-25 10:53:16.057 2015-10-31 10:53:16.057 1
    42  2015-10-11 11:23:57.253 2015-10-17 11:23:57.253 1
    40  2015-09-27 09:04:39.063 2015-10-03 09:04:39.063 1
    44  2015-10-25 10:44:38.573 2015-10-31 10:44:38.573 1
    44  2015-10-25 14:33:31.803 2015-10-31 14:33:31.803 1
    44  2015-10-25 08:18:28.020 2015-10-31 08:18:28.020 1
    44  2015-10-25 12:19:21.270 2015-10-31 12:19:21.270 1

那么如何才能正确地为查询中的每一行添加BeginDateEndDate值?

3 个答案:

答案 0 :(得分:1)

请尝试以下代码:

SELECT 
DATEPART(wk, DateCreated) AS WeekNumber, 
DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + year(datecreated)) + (DATEPART(wk, DateCreated)-1), 6) AS StartOfWeek,
DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + year(datecreated)) + (DATEPART(wk, DateCreated)-1), 5) AS EndOfWeek
COUNT(0) AS WeekCount 
FROM 
SessionTable
WHERE  
DateCreated >= '10/02/2015'  
AND CONVERT(DATE, DateCreated) <= '10/30/2015' 
GROUP BY 
DATEPART(wk, DateCreated)

更正

您应该将startofweek和endofweek转换为date数据类型,然后在group by子句中添加相同的数据类型。同样如下:

SELECT 
DATEPART(wk, DateCreated) AS WeekNumber, 
CAST(DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + year(datecreated)) + (DATEPART(wk, DateCreated)-1), 6) AS DATE) AS StartOfWeek,
CAST(DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + year(datecreated)) + (DATEPART(wk, DateCreated)-1), 5) AS DATE)  AS EndOfWeek
COUNT(0) AS WeekCount 
FROM SessionTable
WHERE DateCreated >= '10/02/2015' AND CONVERT(DATE, DateCreated) <= '10/30/2015' 
GROUP BY DATEPART(wk, DateCreated),
CAST(DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + year(datecreated)) + (DATEPART(wk, DateCreated)-1), 6) AS DATE),
CAST(DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + year(datecreated)) + (DATEPART(wk, DateCreated)-1), 5) AS DATE) 

更新2:

SELECT 
DATEPART(wk, DateCreated) AS WeekNumber, 
CAST(DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + cast(year(datecreated) as varchar)) + (DATEPART(wk, DateCreated)-1), 6) AS DATE) AS StartOfWeek,
CAST(DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + cast(year(datecreated) as varchar)) + (DATEPART(wk, DateCreated)-1), 5) AS DATE)  AS EndOfWeek
COUNT(0) AS WeekCount 
FROM SessionTable
WHERE DateCreated >= '10/02/2015' AND CONVERT(DATE, DateCreated) <= '10/30/2015' 
GROUP BY DATEPART(wk, DateCreated),
CAST(DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + cast(year(datecreated) as varchar)) + (DATEPART(wk, DateCreated)-1), 6) AS DATE),
CAST(DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + cast(year(datecreated) as varchar)) + (DATEPART(wk, DateCreated)-1), 5) AS DATE) 

现在应该可以了。

答案 1 :(得分:1)

如果你像这样修改你的查询,你应该得到想要的结果。 问题是你按DateCreated分组,包括时间。 Cast to Date解决了这个问题。

    SELECT 
    DATEPART(wk, DateCreated) AS WeekNumber, 
    DATEADD(dd, -(DATEPART(dw, CAST(DateCreated AS Date))-1), CAST(DateCreated AS Date)) as [WeekStart],
    DATEADD(dd, 7-(DATEPART(dw, CAST(DateCreated AS Date))), CAST(DateCreated AS Date))  as [WeekEnd],
    COUNT(0) AS WeekCount 
    FROM 
    Session
    WHERE  
    DateCreated >= '10/02/2015'  
    AND CONVERT(DATE, DateCreated) <= '10/30/2015' 
    GROUP BY 
    DATEPART(wk, DateCreated),
    DATEADD(dd, -(DATEPART(dw, CAST(DateCreated AS Date))-1), CAST(DateCreated AS Date)),
    DATEADD(dd, 7-(DATEPART(dw, CAST(DateCreated AS Date))), CAST(DateCreated AS Date))

答案 2 :(得分:0)

我不知道如何在sql中实现。但你可以试试这个:

Enumerable.Range(0, 7)
    .Select(i => "DateTime")
    .ToArray()