create table table1 (Name varchar(20),[Month] varchar(10), [Year] varchar(10) , qty int, [Unique_Number] int);
insert into table1 values
('A','October','2013',30000,4986975),
('B','October','2013',4378,2439257),
('C','October','2013',14575,8378872),
('D','October','2013',2400,1756443),
('A','January','2014',20000,175236),
('D','January','2014', 1500,1236978),
('A','February','2014', 2000,1897606);
select tblQty.Name,[October-2013-QTY],[October-2013-NUM],[January-2014-QTY],[January-2014-NUM],[February-2014-QTY],[February-2014-NUM] from
(select Name,
isnull([October-2013],0) as[October-2013-QTY] ,
isnull([January-2014],0) as[January-2014-QTY],
isnull([February-2014],0) as [February-2014-QTY]
from
(select Name , [Month]+'-'+[Year] as SpreadCol , qty
from table1 ) as D
pivot (sum(qty)
for SpreadCol
IN ([October-2013],[January-2014],[February-2014]))as Pivot1) tblQty
inner join
(
select Name,
isnull([October-2013],0) as[October-2013-NUM] ,
isnull([January-2014],0) as[January-2014-NUM],
isnull([February-2014],0) as [February-2014-NUM]
from
(select Name , [Month]+'-'+[Year] as SpreadCol , Unique_Number
from table1 ) as D
pivot (MAx(Unique_Number)
for SpreadCol
IN ([October-2013],[January-2014],[February-2014]))as Pivot2 ) tblNum
on tblNum.Name = tblQty.Name ;
[编辑:具有所需排序的动态查询]:
DECLARE @columns NVARCHAR(MAX)
,@columnsQTY NVARCHAR(MAX)
,@columnsNUM NVARCHAR(MAX)
,@columnsFNL NVARCHAR(MAX)
,@sql NVARCHAR(MAX);
SET @columns = N'';
--Get column names for entire pivoting
SELECT @columns += N', ' + QUOTENAME(SpreadCol)
FROM (select distinct [Month]+'-'+[Year] as SpreadCol
from table1
) AS T;
PRINT @columns;
--Get column names for Pivot1
SET @columnsQTY = N'';
SELECT @columnsQTY += N', ISNULL(' + QUOTENAME(SpreadCol) + ',0) AS [' + SpreadCol + '-QTY]'
FROM (select distinct [Month]+'-'+[Year] as SpreadCol
from table1
) AS T
;
PRINT @columnsQTY;
--Get column names for Pivot2
SET @columnsNUM = N'';
SELECT @columnsNUM += N', ISNULL(' + QUOTENAME(SpreadCol) + ',0) AS [' + SpreadCol + '-NUM]'
FROM (select distinct [Month]+'-'+[Year] as SpreadCol
from table1
) AS T
;
PRINT @columnsNUM;
--Get final list of columns:
SET @columnsFNL = N'';
SELECT @columnsFNL += N', [' + SpreadCol + '-QTY], [' + SpreadCol + '-NUM] '
FROM (select distinct [Month]+'-'+[Year] as SpreadCol,
DATEPART(MM, Upper([Month]) + ' 01 1990') as [MonthNum] ,[Year]
from table1
) AS T
order by T.[Year] asc , T.[MonthNum] desc; -- change ordering of columns here
PRINT @columnsFNL;
SET @sql = N'
select tblQty.Name, ' + STUFF(@columnsFNL, 1, 2, '') + ' from
'
+
'
( SELECT Name, ' + STUFF(@columnsQTY, 1, 2, '') + '
FROM
(select Name , [Month]+''-''+[Year] as SpreadCol , qty
from table1 ) as D
PIVOT
(
sum(qty) FOR SpreadCol IN ('
+ STUFF(REPLACE(@columns, ', [', ',['), 1, 1, '')
+ ')
) AS Pivot1 ) tblQty
inner join
'
+
'
( SELECT Name, ' + STUFF(@columnsNUM, 1, 2, '') + '
FROM
(select Name , [Month]+''-''+[Year] as SpreadCol , Unique_Number
from table1 ) as D
PIVOT
(
MAx(Unique_Number) FOR SpreadCol IN ('
+ STUFF(REPLACE(@columns, ', [', ',['), 1, 1, '')
+ ')
) as Pivot2 ) tblNum
on tblNum.Name = tblQty.Name ;
'
;
PRINT @sql;
EXEC sp_executesql @sql;
希望这可以帮助!!!