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-枢轴:
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)
问题,我有 2 个字段需要旋转,amount_currency1 和amount_currency2