如何转动

时间:2015-04-29 09:12:07

标签: sql-server

我只是不了解旋转,我需要使用UNION进行简单查询的帮助。我的查询给了我作为研究员的结果:

SELECT 
  'LuFt' AS [Source],
   SUM([SalesAmount1]) AS [Sales old],
   SUM([SalesAmount2]) AS [Sales new]
FROM [dbo].[tblSales]
WHERE ([Source] IN ('LuFt')) AND [Planung] = 'kosten'

UNION

SELECT 
  'TGL' AS [Source],
   SUM([SalesAmount1]) AS [Sales old],
   SUM([SalesAmount2]) AS [Sales new]
FROM [dbo].[tblSales]
WHERE ([Source] IN ('TGL')) AND [Planung] = 'kosten'

UNION

SELECT 
  'BHW' AS [Source],
   SUM([SalesAmount1]) AS [Sales old],
   SUM([SalesAmount2]) AS [Sales new]
FROM [dbo].[tblSales]
WHERE ([Source] IN ('BHW')) AND [Planung] = 'kosten'

UNION

SELECT 
   'WM ' AS [Source],
    SUM([SalesAmount1]) AS [Sales old],
    SUM([SalesAmount2]) AS [Sales new]
FROM [dbo].[tblSales]
WHERE ([Source] IN ('WM')) AND [Planung] = 'kosten'

结果:

[Source]      [Sales old]      [Sales new]
 Luft           10000            20000
 TGL            500              800
 BHW            1500             500
 WM             700              400

使用pivot的我的期望:

[]          [Luft]   [TGL]  [BHW]  [WM]
Sales old    10000    500    1500   700
Sales new    20000    800    500    400

3 个答案:

答案 0 :(得分:1)

首先为每个来源和销售类型添加一行会更容易:

select * from 
(
        select [Source], [SalesAmount1] as [Sales old], [SalesAmount2] as [Sales new] 
        FROM tblSales
        WHERE [Planung] = 'Kosten' 
) as src
UNPIVOT ([Amount] for [Type] in ([Sales old], [Sales new])) up

这会让你

Source  Amount  Type       
------  ------  ---------  
LuFt    3000    Sales old  
LuFt    9000    Sales new  
LuFt    7000    Sales old  
LuFt    11000   Sales new  
TGL     500     Sales old  
TGL     800     Sales new  
BHW     1500    Sales old  
BHW     500     Sales new  
WM      700     Sales old  
WM      400     Sales new   
...

然后你可以更容易地转向它:

declare @tblSales table(Source varchar(20), SalesAmount1 int, SalesAmount2 int, Planung varchar(30))
insert into @tblSales values ('LuFt', 3000, 9000, 'Kosten')
insert into @tblSales values ('LuFt', 7000, 11000, 'Kosten')
insert into @tblSales values ('TGL', 500, 800, 'Kosten')
insert into @tblSales values ('BHW', 1500, 500, 'Kosten')
insert into @tblSales values ('WM',  700, 400, 'Kosten')

select Source, [SalesAmount1] as [Sales old], [SalesAmount2] as [Sales new] 
FROM @tblSales
WHERE [Planung] = 'Kosten' 

select * from 
(
        select [Source], [SalesAmount1] as [Sales old], [SalesAmount2] as [Sales new] 
        FROM @tblSales
        WHERE [Planung] = 'Kosten' 
) as src
UNPIVOT ([Amount] for [Type] in ([Sales old], [Sales new])) up
PIVOT (sum([Amount]) for [Source] in ([LuFt], [TGL], [BHW], [WM])) p

结果:

type       LuFt   TGL  BHW   WM   
---------  -----  ---  ----  ---  
Sales old  10000  500  1500  700  
Sales new  20000  800  500   400  

答案 1 :(得分:1)

也许,

SELECT [Type] as '[]', [Luft], [TGL], [BHW], [WM] FROM 
(
SELECT 'Sales old' as [Type], [SalesAmount1] as [SalesAmount], [Source] FROM [dbo].[tblSales]
UNION
SELECT 'Sales new' as [Type], [SalesAmount2] as [SalesAmount], [Source] FROM [dbo].[tblSales]
) AS SourceTable

PIVOT

(
   SUM([SalesAmount]) 
   FOR [Source] IN ([Luft], [TGL], [BHW], [WM])

) AS PivotTable

答案 2 :(得分:0)

您使用union将两个select语句一起添加。结果就是这样的原因,就是您选择来源和旧/新销售作为您的列。

对于所需的输出,我会写如下:

如果您对该方法或查询有任何疑问,请随时询问。

SELECT 
  'Sales old' AS [Category],
  SUM(Case when [Source] = 'Luft' then [SalesAmount1] else 0 end) AS [Luft],
  SUM(Case when [Source] = 'TGL' then [SalesAmount1] else 0 end) AS [TGL],
  SUM(Case when [Source] = 'BHW' then [SalesAmount1] else 0 end) AS [BHW],
  SUM(Case when [Source] = 'WM' then [SalesAmount1] else 0 end) AS [WM]
FROM [dbo].[tblSales]
WHERE [Planung] = 'kosten'

UNION

SELECT 
  'Sales new' AS [Category],
  SUM(Case when [Source] = 'Luft' then [SalesAmount2] else 0 end) AS [Luft],
  SUM(Case when [Source] = 'TGL' then [SalesAmount2] else 0 end) AS [TGL],
  SUM(Case when [Source] = 'BHW' then [SalesAmount2] else 0 end) AS [BHW],
  SUM(Case when [Source] = 'WM' then [SalesAmount2] else 0 end) AS [WM]
FROM [dbo].[tblSales]
WHERE [Planung] = 'kosten'