选择按周分组的SQL结果

时间:2011-06-20 10:57:25

标签: sql-server tsql sql-server-2005

我希望按周列出以下表格组中的数据

 Date       Product Name   Sale
+----------+--------------+-----+
 14-05-11     a             2
 14-05-11     b             4 
 17-05-11     c             3
 19-05-11     a             6
 24-05-11     a             6
 29-05-11     a             6    

我们假设今天是30-05-11

所以我的结果应该是这样的。

 Product Name         First Week   Second Week  Third Week
+--------------------+------------+------------+-------------+
   a                      12            6           2
   b                       0            0           4 
   c                       0            3           0  

有些人会指导我如何编写SQL查询来实现这种行为!

5 个答案:

答案 0 :(得分:36)

我认为应该这样做..

Select 
ProductName,
WeekNumber,
sum(sale)
from
(
    SELECT 
    ProductName,
    DATEDIFF(week, '2011-05-30', date) AS WeekNumber,
    sale
    FROM table
)
GROUP BY
ProductName,
WeekNumber

答案 1 :(得分:13)

这应该适合你:

Declare @DatePeriod datetime

Set @DatePeriod = '2011-05-30'

Select  ProductName,
        IsNull([1],0) as 'Week 1',
        IsNull([2],0) as 'Week 2',
        IsNull([3],0) as 'Week 3',
        IsNull([4],0) as 'Week 4',
        IsNull([5], 0) as 'Week 5'

From 
(
Select  ProductName,
        DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, InputDate), 0), InputDate) +1 as [Weeks],
        Sale as 'Sale'

From dbo.YourTable
-- Only get rows where the date is the same as the DatePeriod
-- i.e DatePeriod is 30th May 2011 then only the weeks of May will be calculated
Where DatePart(Month, InputDate)= DatePart(Month, @DatePeriod)
)p 
Pivot (Sum(Sale) for Weeks in ([1],[2],[3],[4],[5])) as pv

它将计算相对于月份的周数。因此,而不是一年中的第20周,它将是第2周。@DatePeriod变量仅用于获取相对于月份的行(在此示例中仅针对5月份)

使用我的示例数据输出:

enter image description here

答案 2 :(得分:4)

提供的解决方案看起来有点复杂?这可能会有所帮助:

https://msdn.microsoft.com/en-us/library/ms174420.aspx

select
   mystuff,
   DATEPART ( year, MyDateColumn ) as yearnr,
   DATEPART ( week, MyDateColumn ) as weeknr
from mytable
group by ...etc

答案 3 :(得分:0)

Declare @DatePeriod datetime
Set @DatePeriod = '2011-05-30'

Select  ProductName,
        IsNull([1],0) as 'Week 1',
        IsNull([2],0) as 'Week 2',
        IsNull([3],0) as 'Week 3',
        IsNull([4],0) as 'Week 4',
        IsNull([5], 0) as 'Week 5'
From 
(
Select  ProductName,
        DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, '2011-05-30'), 0), '2011-05-30') +1 as [Weeks],
        Sale as 'Sale'
From dbo.WeekReport

-- Only get rows where the date is the same as the DatePeriod
-- i.e DatePeriod is 30th May 2011 then only the weeks of May will be calculated
Where DatePart(Month, '2011-05-30')= DatePart(Month, @DatePeriod)
)p 
Pivot (Sum(Sale) for Weeks in ([1],[2],[3],[4],[5])) as pv

输出看起来像这个

a   0   0   0   0   20
b   0   0   0   0   4
c   0   0   0   0   3

答案 4 :(得分:0)

基于@increddibelly的答案,我将查询应用于如下。

我与谁分享。

我的表结构FamilyData(Id, nodeTime, totalEnergy)

select
   sum(totalEnergy) as TotalEnergy,
   DATEPART ( week, nodeTime ) as weeknr
from FamilyData
group by DATEPART (week, nodeTime)