ID ---- ACCOUNT ---- SECTOR ---- AMOUNT_CURRENCY1 ------ AMOUNT_CURRENCY2
1 --- account1 ---- sector1 --- 100 ------ 200
2 --- account1 ---- sector2 --- 150 ------ 250
3 --- account2 ---- sector1 --- 250 ------ 300
4 --- account2 ---- sector2 --- 90 ------ 180
我需要数据像这样
sector1 ------------ sector2
| amount1 | amount2 | amount1 | amount2
Account1 | 100 | 200
Account2 | 250 | 250
我需要将结果放在asp.net gridview中进行编辑
我正在使用以下脚本: 1-获取专栏:
DECLARE @ColumnHeaders VARCHAR(MAX)
SELECT @ColumnHeaders =
COALESCE(
@ColumnHeaders + ',[!sector:' + cast(sector_ID as nvarchar)+ ':' + sector_name + ']',
'[!sector:' + cast(sector_ID as nvarchar)+ ':' + sector_name+ ']'
)
FROM vw_Transaction
group by sector_ID, sector_name
2- pivot:
DECLARE @TableSQL NVARCHAR(MAX)
SET @TableSQL = N'
SELECT *
FROM (SELECT trans_id, account_name, sector_id, sector_name,
amount_currency1, amount_currency2, ''!sector:'' + cast(sector_ID as nvarchar)+ '':'' + sector_name as col
FROM dbo.vw_Transaction
WHERE
trans_id=' + CAST(@trans_id as varchar) +'
) AS PivotData
PIVOT (
MAX(amount_currency1)
FOR col IN (
' + @ColumnHeaders + '
)
) AS PivotTable'
EXECUTE(@TableSQL)
问题,我有两个需要转动的字段,amount_currency1和amount_currency2
答案 0 :(得分:0)
由于您尝试PIVOT
两个值,因此您必须先UNPIVOT
AMOUNT_CURRENCY1
和AMOUNT_CURRENCY2
列:
您需要在查询中添加以下内容:
select account, value, sector+ '_'+col as col
from
(
select sector,
account,
AMOUNT_CURRENCY1,
AMOUNT_CURRENCY2
from vw_Transaction
) src
unpivot
(
value
for col in (AMOUNT_CURRENCY1, AMOUNT_CURRENCY2)
) unpiv
然后您的最终查询将与此类似:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT DISTINCT ','
+ quotename(t.sec+'_'+c.name)
from
(
select sector sec
from vw_Transaction
) t
cross apply sys.columns as C
where C.object_id = object_id('vw_Transaction') and
C.name not in ('id', 'account', 'sector')
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT account,' + @cols + ' from
(
select account, value,
sector+ ''_''+col as col
from
(
select sector,
account,
AMOUNT_CURRENCY1,
AMOUNT_CURRENCY2
from vw_Transaction
) src
unpivot
(
value
for col in (AMOUNT_CURRENCY1, AMOUNT_CURRENCY2)
) unpiv
) x
pivot
(
max(value)
for col in (' + @cols + ')
) p '
execute(@query)
结果:
| ACCOUNT | SECTOR1_AMOUNT_CURRENCY1 | SECTOR1_AMOUNT_CURRENCY2 | SECTOR2_AMOUNT_CURRENCY1 | SECTOR2_AMOUNT_CURRENCY2 |
------------------------------------------------------------------------------------------------------------------------
| account1 | 100 | 200 | 150 | 250 |
| account2 | 250 | 300 | 90 | 180 |