如何以周订单显示记录

时间:2014-12-11 10:56:15

标签: sql-server database sql-server-2008 select stored-procedures

我正在拥有包含记录数12000的表格,现在我需要以周为基础显示如下所示

ID            Date                   count
1       4 week of November 2014       10
2       1 week of December 2014       120
3       2 week of December 2014       60

我试过

SELECT CONVERT(VARCHAR(150),datepart(day, datediff(day, 0, date)/7 * 7)/7 + 1) 
+ ' week of ' + CONVERT(VARCHAR(150),datename(month,date)) 
+ ' ' + CONVERT(VARCHAR(150),datepart(YEAR,date))
AS DisplayDate, 
COUNT(date) AS LettersCount
FROM tblV400_Leads_Letters 
WHERE DATEDIFF(DAY,date, GETDATE()) <= 28
GROUP BY date

它给出了

 ID            Date                   count
 1    4 week of November 2014          10
 2    2 week of December 2014          74
 3    2 week of December 2014          78
 4    2 week of December 2014          59
 5    1 week of December 2014          79
 6    1 week of December 2014          68
 7    1 week of December 2014          60
 8    1 week of December 2014          68
 9    1 week of December 2014          83

2 个答案:

答案 0 :(得分:1)

您正在按date列进行分组,这意味着您将为date的每个不同值获得一个记录输出。确保GROUP BY子句中的表达式与实际输出的表达式完全相同:

SELECT
    CONVERT(VARCHAR(150),datepart(day, datediff(day, 0, date)/7 * 7)/7 + 1) 
+ ' week of ' + CONVERT(VARCHAR(150),datename(month,date)) 
+ ' ' + CONVERT(VARCHAR(150),datepart(YEAR,date))
AS DisplayDate, 
COUNT(date) AS LettersCount
FROM tblV400_Leads_Letters 
WHERE DATEDIFF(DAY,date, GETDATE()) <= 28
GROUP BY
    /* This is simply copied from the SELECT part of the query: */
    CONVERT(VARCHAR(150),datepart(day, datediff(day, 0, date)/7 * 7)/7 + 1) 
+ ' week of ' + CONVERT(VARCHAR(150),datename(month,date)) 
+ ' ' + CONVERT(VARCHAR(150),datepart(YEAR,date))

为避免重复代码,您可以将所有内容包装在子选择中:

SELECT DisplayDate, COUNT(*) FROM (
    SELECT CONVERT(VARCHAR(150),datepart(day, datediff(day, 0, date)/7 * 7)/7 + 1) 
    + ' week of ' + CONVERT(VARCHAR(150),datename(month,date)) 
    + ' ' + CONVERT(VARCHAR(150),datepart(YEAR,date)) AS DisplayDate
    FROM tblV400_Leads_Letters 
    WHERE DATEDIFF(DAY,date, GETDATE()) <= 28
) T
GROUP BY DisplayDate

此外,我还不完全确定此代码的作用:CONVERT(VARCHAR(150),datepart(day, datediff(day, 0, date)/7 * 7)/7 + 1)。但是,如果您尝试获取周数,则更简单的解决方案就是使用datepart(week, date)或者datepart(iso_wk, date),如果您居住在欧洲。

答案 1 :(得分:1)

这里有两种不同的方式,两种方式都假设周一开始一周。

如果您的周变为月变化,那么2012-09-01星期六和星期日2012-09-02是第1周,星期一2012-09-03是第2周使用此:

SELECT CAST(datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, date), 0)), 0), date - 1) + 1 AS VARCHAR(1))
+ ' week of ' + CONVERT(VARCHAR(150),datename(month,date)) 
+ ' ' + CONVERT(VARCHAR(150),datepart(YEAR,date))
AS DisplayDate, 
COUNT(date) AS LettersCount
FROM tblV400_Leads_Letters 
WHERE DATEDIFF(DAY,date, GETDATE()) <= 100
GROUP BY CAST(datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, date), 0)), 0), date - 1) + 1 AS VARCHAR(1))
    + ' week of ' + CONVERT(VARCHAR(150),datename(month,date)) 
    + ' ' + CONVERT(VARCHAR(150),datepart(YEAR,date))

如果你想要整周,那么他们属于他们开始的月份:所以星期六2012-09-01和星期日2012-09-02是第4周和星期一2012-09-03是第1周使用这样:

SELECT CAST(datepart(day, datediff(day, 0, date)/7 * 7)/7 + 1 AS VARCHAR(1))
+ ' week of ' + CONVERT(VARCHAR(150),datename(month,date)) 
+ ' ' + CONVERT(VARCHAR(150),datepart(YEAR,date))
AS DisplayDate, 
COUNT(date) AS LettersCount
FROM AECCFORECASTLOG 
WHERE DATEDIFF(DAY,date, GETDATE()) <= 100
GROUP BY CAST(datepart(day, datediff(day, 0, date)/7 * 7)/7 + 1 AS VARCHAR(1))
    + ' week of ' + CONVERT(VARCHAR(150),datename(month,date)) 
    + ' ' + CONVERT(VARCHAR(150),datepart(YEAR,date))