-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

I need data to be like this

               sector1 ------------ sector2
         | amount1 | amount2 |  amount1 | amount2
Account1 | 100     | 200
Account2 | 250     | 250

I need to put the result in asp.net gridview to edit

I'm using the following script: 1- to get columns:

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)

the problem, i have 2 fields that need to pivot, amount_currency1 and amount_currency2

Taryn
  • 242,637
  • 56
  • 362
  • 405
Abady
  • 111
  • 1
  • 2
  • 10

1 Answers1

0

Since you are trying to PIVOT on two values, you must first UNPIVOT the AMOUNT_CURRENCY1 and AMOUNT_CURRENCY2 columns:

You will want to add something like this to your query:

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

Then your final query will be similar to this:

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)

See SQL Fiddle with Demo

Result:

|  ACCOUNT | SECTOR1_AMOUNT_CURRENCY1 | SECTOR1_AMOUNT_CURRENCY2 | SECTOR2_AMOUNT_CURRENCY1 | SECTOR2_AMOUNT_CURRENCY2 |
------------------------------------------------------------------------------------------------------------------------
| account1 |                      100 |                      200 |                      150 |                      250 |
| account2 |                      250 |                      300 |                       90 |                      180 |
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • This works great, i have a question, Is there anyway to add ID, because i need it for Update operation? Thank you – Abady Dec 05 '12 at 20:49
  • @user1450667 the problem you will have is the `pivot` will not work the same if you add the id, especially since you have different ids per record. – Taryn Dec 05 '12 at 20:56