-2
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

4

1 回答 1

0

由于您尝试PIVOT使用两个值,因此您必须首先UNPIVOT使用AMOUNT_CURRENCY1andAMOUNT_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)

请参阅带有演示的 SQL Fiddle

结果:

|  ACCOUNT | SECTOR1_AMOUNT_CURRENCY1 | SECTOR1_AMOUNT_CURRENCY2 | SECTOR2_AMOUNT_CURRENCY1 | SECTOR2_AMOUNT_CURRENCY2 |
------------------------------------------------------------------------------------------------------------------------
| account1 |                      100 |                      200 |                      150 |                      250 |
| account2 |                      250 |                      300 |                       90 |                      180 |
于 2012-12-05T19:39:16.737 回答