2013-05-01 75 views
0

我正在使用SQLServer2008。在存儲過程中,我計算了一些數量並存儲在@tmp表中,然後從@tmp表中獲取數據。以水平格式顯示數據

SELECT t.BrokerCode, 
     t.PlanYear , 
     t.PlanName , 
     SUM(Fresh) AS 'Fresh' , 
FROM @tmp AS t 
GROUP BY t.PlanYear , 
     t.PlanName , 
     t.CscName , 
     t.BrokerCode 

這給我的結果,如:

------------------------------------------------- 
BrokerCode | PlanYear | PlanName | Fresh  
------------------------------------------------- 
    106  | 3  | SLP |  0.00 
    106  | 3  | MLP | 1140.00 
    106  | 5  | MLP | 570.00 
    205  | 4  | SLP | 450.00 

現在我想顯示的數據爲:

---------------------------------------------------------- 
      |  SLP   |   MLP   | 
--------------------------------------------------------- 
BrokerCode |  3  | 4  |  3 |  5  | 
---------------------------------------------------------- 
    106  | 0.00 | 0.00 | 1140.00 | 570.00 | 
    205  | 0.00 | 450.00 |  0.00 | 0.00 | 

我heared繞樞軸查詢,但我沒有的很多知識樞軸查詢,但我仍然嘗試使用以下查詢

SELECT * 
FROM 
(
    SELECT [PlanYear], [BrokerCode], [Fresh] 
    FROM @tmp 
) AS source 
PIVOT 
(
    sum([Fresh]) 
    FOR [PlanYear] IN ([3], [4], [5]) 
) as pvt 

所以它給我的結果:

---------------------------------------------- 
BrokerCode |  3  | 4  |  5 | 
---------------------------------------------- 
    106  | 1140.00 | 0.00 | 570.00 | 
    205  | 0.00 | 450.00 |  0.00 | 

但我的問題是,PlanYear可以是任何東西。

那麼我該如何做到這一點?謝謝。

回答

1

有兩個級別的標題(計劃名稱>年)是顯示問題,而不是查詢問題。但是,您可以按照以下方式通過合併PIVOT中的planname和planyear列來壓扁標題。這項技術的搜索條件是「動態數據透視」。


SQL Fiddle

create table #tmp (
    BrokerCode int, 
    PlanYear int, 
    PlanName char(3), 
    Fresh decimal(10,4)); 
insert #tmp select 
    106  , 3  , 'SLP' ,  0.00 union all select 
    106  , 3  , 'MLP' , 1140.00 union all select 
    106  , 5  , 'MLP' , 570.00 union all select 
    205  , 4  , 'SLP' , 450.00; 

declare @sql nvarchar(max); 
select @sql = isnull(@sql+',','') + 
       quotename(PlanName+'-'+right(PlanYear,10)) 
from (select distinct PlanName from #tmp) a 
cross join (select distinct PlanYear from #tmp) b 
order by PlanName, PlanYear; 

set @sql = ' 
SELECT * 
FROM 
(
    SELECT PlanName+''-''+right(PlanYear,10) [PlanYear], 
      [BrokerCode], [Fresh] 
    FROM #tmp 
) AS source 
PIVOT 
(
    sum([Fresh]) 
    FOR [PlanYear] IN ('[email protected]+') 
) as pvt'; 

exec(@sql); 

結果

| BROKERCODE | MLP-3 | MLP-4 | MLP-5 | SLP-3 | SLP-4 | SLP-5 | 
-------------------------------------------------------------------- 
|  106 | 1140 | (null) | 570 |  0 | (null) | (null) | 
|  205 | (null) | (null) | (null) | (null) | 450 | (null) | 

如果你真的想你的問題的最終輸出,然後the below variation會做。

declare @sql nvarchar(max); 
select @sql = isnull(@sql+',','') + 
       quotename(right(PlanYear,10)) 
from (select distinct PlanYear from #tmp) b 
order by PlanYear; 

set @sql = ' 
SELECT * 
FROM 
(
    SELECT [PlanYear], [BrokerCode], [Fresh] 
    FROM #tmp 
) AS source 
PIVOT 
(
    sum([Fresh]) 
    FOR [PlanYear] IN ('[email protected]+') 
) as pvt'; 

----------------------------------------- 
| BROKERCODE |  3 |  4 |  5 | 
----------------------------------------- 
|  106 | 1140 | (null) | 570 | 
|  205 | (null) | 450 | (null) |