[英]SQL Server Pivot multiple columns based on rows from another table
I have following two source and one destination tables in sql server 2008R2. 我在SQL Server 2008R2中具有以下两个源表和一个目标表。 How can I do pivot(s) in TSQL to get to the destination from sources? 如何在TSQL中进行数据透视以从源到达目标? I need it urgently. 我迫切需要它。
Source Table 1: ProdType 源表1:ProdType
key name
--------------
1 Magazines
2 Journals
3 Books
4 Newspaper
Source Table 2: Orders 来源表2:订单
seqno ODate Key Qty UnitPrice
--------------------------------------------------
1 2013-10-12 1 10 5
2 2013-10-12 4 20 3
3 2013-10-13 2 5 3
4 2013-10-14 4 50 5
5 2013-10-15 1 100 2.5
Destination Table: Orders Detail 目的地表:订单明细
Odate Magazine Journals Books Newspaper
-----------------------------------------------------------------
12/10/2013 10 5 0 0 0 0 20 3
13/10/2013 0 0 5 3 0 0 0 0
14/10/2013 0 0 0 0 0 0 50 5
15/10/2013 100 2.5 0 0 0 0 0 0
-----------------------------------------------------------------
NOTE *qty *unit price
Any help would be greatly appreciated! 任何帮助将不胜感激! As you can tell, I'm quite new to T-SQL (or SQL in general) and SQL Server. 如您所知,我对T-SQL(或通常的SQL)和SQL Server还是很陌生。 Thanks in advance! 提前致谢!
with cte as (
select
O.ODate, P.Name,
sum(O.Qty) as Qty,
sum(O.UnitPrice * O.Qty) / sum(O.Qty) as UnitPrice
from Orders as O
inner join ProdType as P on P.Id = O.Id
group by O.ODate, P.Name
)
select
ODate,
max(case when Name = 'Magazines' then Qty else 0 end) as Magazines_Qty,
max(case when Name = 'Magazines' then UnitPrice else 0 end) as Magazines_UnitPrice,
max(case when Name = 'Journals' then Qty else 0 end) as Journals_Qty,
max(case when Name = 'Journals' then UnitPrice else 0 end) as Journals_UnitPrice,
max(case when Name = 'Books' then Qty else 0 end) as Books_Qty,
max(case when Name = 'Books' then UnitPrice else 0 end) as Books_UnitPrice,
max(case when Name = 'Newspaper' then Qty else 0 end) as Newspaper_Qty,
max(case when Name = 'Newspaper' then UnitPrice else 0 end) as Newspaper_UnitPrice
from cte
group by ODate
Or dynamic, if you want to: 或动态的,如果您想:
declare @stmt nvarchar(max)
select @stmt =
isnull(@stmt + ', ', '') +
'max(case when Name = ''' + name + ''' then Qty else 0 end) as ' + quotename(name + '_Qty') + ',' +
'max(case when Name = ''' + name + ''' then UnitPrice else 0 end) as ' + quotename(name + '_UnitPrice')
from ProdType
select @stmt = '
with cte as (
select
O.ODate, P.Name,
sum(O.Qty) as Qty,
sum(O.UnitPrice * O.Qty) / sum(O.Qty) as UnitPrice
from Orders as O
inner join ProdType as P on P.Id = O.Id
group by O.ODate, P.Name
)
select
ODate, ' + @stmt + ' from cte group by ODate'
exec dbo.sp_executesql
@stmt = @stmt
If you don't mind having qty and unit price in different tables, here's a sample query for the qty destination table: 如果您不介意在不同的表中包含数量和单价,则以下是数量目标表的示例查询:
;with cte as
(
SELECT ODate, Qty, UnitPrice, name FROM Orders INNER JOIN ProdType ON Orders.[Key] = ProdType.[Key]
)
SELECT ODate, [Magazines], [Journals], [Books], [Newspaper]
FROM cte
PIVOT
(
SUM(Qty)
FOR name IN ([Magazines], [Journals], [Books], [Newspaper])
) AS QTYPivot
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.