我在SQL中有这个表。 [Tbl_SALES]
+-------------+-----------+-------------+---------+-------+------+-------+
| Cust_Number |Code_Number| PartNumber | UserID | Month | Year | Sales |
+-------------+-----------+-------------+---------+-------+------+-------+
| 5535536 | 101 | AB11111 | Luis | Aug | 16 | 100 |
| 9256819 | 101 | AV11111 | Luis | Aug | 16 | 100 |
| 4369983 | 101 | AX11111 | Luis | Aug | 16 | 100 |
| 5535536 | 101 | AB11111 | Luis | Sep | 16 | 200 |
| 9256819 | 101 | AV11111 | Luis | Sep | 16 | 200 |
| 4369983 | 101 | AX11111 | Luis | Sep | 16 | 200 |
| 5535536 | 101 | AB11111 | Luis | Oct | 16 | 100 |
| 4369983 | 101 | AX11111 | Luis | Oct | 16 | 200 |
| 6441842 | 101 | AA11111 | Luis | Oct | 16 | 200 |
| 5535536 | 101 | AB11111 | Luis | Nov | 16 | 100 |
| 5535536 | 101 | AB11111 | Luis | Dec | 16 | 100 |
| 6441842 | 101 | AA11111 | Luis | Dec | 16 | 100 |
| 5535536 | 101 | AB11111 | Luis | Jan | 17 | 100 |
| 6441842 | 101 | AA11111 | Luis | Jan | 17 | 100 |
| 4369983 | 101 | AX11111 | Luis | Feb | 17 | 300 |
| 6441842 | 101 | AA11111 | Luis | Feb | 17 | 100 |
+-------------+-----------+-------------+---------+-------+------+-------+
我需要将Month行转换为Columns。 我有这个SQL查询,可以正确地转置所有月份。
SELECT
[Cust_Number],
[Code_Number],
[PartNumber],
[UserID],
SUM([Aug'16]) as [Aug'16],
SUM([Sep'16]) as [Sep'16],
SUM([Oct'16]) as [Oct'16],
SUM([Nov'16]) as [Nov'16],
SUM([Dec'16]) as [Dec'16],
SUM([Jan'17]) as [Jan'17],
SUM([Feb'17]) as [Feb'17],
SUM([Mar'17]) as [Mar'17],
SUM([Apr'17]) as [Apr'17],
SUM([May'17]) as [May'17],
SUM([Jun'17]) as [Jun'17],
SUM([Jul'17]) as [Jul'17]
FROM (
SELECT
[Cust_Number],
[Code_Number],
[PartNumber],
[UserID],
case when ([MONTH] = 'Aug') then SUM([SALES]) else Null end as [Aug'16],
case when ([MONTH] = 'Sep') then SUM([SALES]) else Null end as [Sep'16],
case when ([MONTH] = 'Oct') then SUM([SALES]) else Null end as [Oct'16],
case when ([MONTH] = 'Nov') then SUM([SALES]) else Null end as [Nov'16],
case when ([MONTH] = 'Dec') then SUM([SALES]) else Null end as [Dec'16],
case when ([MONTH] = 'Jan') then SUM([SALES]) else Null end as [Jan'17],
case when ([MONTH] = 'Feb') then SUM([SALES]) else Null end as [Feb'17],
case when ([MONTH] = 'Mar') then SUM([SALES]) else Null end as [Mar'17],
case when ([MONTH] = 'Apr') then SUM([SALES]) else Null end as [Apr'17],
case when ([MONTH] = 'May') then SUM([SALES]) else Null end as [May'17],
case when ([MONTH] = 'Jun') then SUM([SALES]) else Null end as [Jun'17],
case when ([MONTH] = 'Jul') then SUM([SALES]) else Null end as [Jul'17]
FROM [Test].[dbo].[Tbl_SALES]
where [UserID] = 'Luis'
group by
[Cust_Number],
[Code_Number],
[PartNumber],
[UserID],
[MONTH]
) a
group by
[Cust_Number],
[Code_Number],
[PartNumber],
[UserID]
查询结果。
+---------------+-------------+-------------+------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+----------+
| Cust_Name | Code_Number | PartNumber | User | Aug'16 | Sep'16 | Oct'16 | Nov'16 | Dec'16 | Jan'17 | Feb'17 | Mar'17 | Apr'17 | May'17 | Jun'17 | Jul'17 | |
+---------------+-------------+-------------+------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+----------+
| 5535536 | 101 | AB11111 | Luis | 100 | 200 | 100 | 100 | 100 | 100 | | | | | | |
| 9256819 | 101 | AV11111 | Luis | 100 | 200 | | | | | | | | | | |
| 4369983 | 101 | AX11111 | Luis | 100 | 200 | 200 | | | | 300 | | | | | |
| 6441842 | 101 | AA11111 | Luis | | | 200 | | 100 | 100 | 100 | | | | | |
+---------------+-------------+-------------+------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+----------+
考虑先前的查询...如何根据当前日期使此查询动态化? 让我们说,今天是2016年8月28日,那么案例列表中的第一个月应该是8月16日,但是,您是否知道如何改进这一点并自动进行而不是更改它每个月由我?
答案 0 :(得分:0)
您需要的是Pivot Tables
他们支持动态列
更多例子: https://blogs.msdn.microsoft.com/spike/2009/03/03/pivot-tables-in-sql-server-a-simple-sample/
另请看这篇SO帖子。它显示了动态生成周数的示例,但您可以轻松地将其采用到几个月: Convert Rows to columns using 'Pivot' in SQL Server