SQL SERVER PIVOT TABLE One Row -


i have table e.g.

  pay    staff    amount       tax    1       40     10.00        1.5    1       40     20.00        3.0    1       40     15.00        2.0 

i want output table e.g.

pay  staff amount1 tax1    amount2 tax2   amount3    tax3  1    40     10    1.5       20   3.0      15.00     2.0 

how please?

you can using dynamic crosstab. read article jeff moden reference:

sql fiddle

declare @sql1 varchar(4000) = '' declare @sql2 varchar(4000) = '' declare @sql3 varchar(4000) = ''  select @sql1 = 'select       pay     , staff ' select @sql2 = @sql2 + '   , max(case when rn = ' + convert(varchar(4), rn) + ' amount end) [amount' + convert(varchar(4), rn) + ']' + char(10) + '   , max(case when rn = ' + convert(varchar(4), rn) + ' tax end) [tax' + convert(varchar(4), rn) + ']' + char(10) from(     select distinct row_number() over(order (select null)) rn     yourtable )t  select @sql3 = 'from(     select *, rn=row_number() over(order (select null))     yourtable )t group pay, staff order pay, staff'  print (@sql1 + @sql2 + @sql3) exec (@sql1 + @sql2 + @sql3) 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -