sql server - Pivot SQL table (Rows into Columns) -


i returning data table in sql stored procedure. trying pivot rows columns, , columns rows, struggling lot of tutorials following have laid out they're tables differently.

this select @ bottom of stored procedure:

select   (case when [fitter] null (select distinct substring([first name],1,1)+' '+[second name] fitters [fitter id]=fitterid) else fitter end) fitter, sum([install sell]) [install sell], sum([install cost]) [install cost], sum([install cost amt]) gross, (select cast(cast((taxstatus/100 * sum([install cost amt])) decimal(18,5)) float)) cis, (select cast(cast((vatstatus/100 * sum([install cost amt])) decimal(18,5)) float)) vat, sum([install cost amt]) - (select cast(cast((taxstatus/100 * sum([install cost amt])) decimal(18,5)) float)) + (select cast(cast((vatstatus/100 * sum([install cost amt])) decimal(18,5)) float)) fitterspay, sum([install cost amt]) + (select cast(cast((vatstatus/100 * sum([install cost amt])) decimal(18,5)) float)) datafile  @temptable group fitterid, fitter, taxstatus, vatstatus 

this data returns: enter image description here

i columns pivot rows , rows pivot columns...

i have searched around online , struggling figure out, wondering if assistance please.

any assistance appreciated.

this done first unpivoting , pivoting. here example, can adjust yo data:

declare @t table(col0 varchar(20), col1 money, col2 money, col3 money)  insert @t values ('aaaaaa', 1, 1.2, 0), ('bbbbbb', 2, 2.2, 0), ('cccccc', 3, 3.3, 100), ('dddddd', 4, 4.4, 0)  select * @t   select * @t unpivot (a b in([col1],[col2],[col3])) pivot (max(a) col0 in([aaaaaa],[bbbbbb],[cccccc],[dddddd])) p 

output1:

col0    col1    col2    col3 aaaaaa  1.00    1.20    0.00 bbbbbb  2.00    2.20    0.00 cccccc  3.00    3.30    100.00 dddddd  4.00    4.40    0.00 

output2:

b       aaaaaa  bbbbbb  cccccc  dddddd col1    1.00    2.00    3.00    4.00 col2    1.20    2.20    3.30    4.40 col3    0.00    0.00    100.00  0.00 

it depends on type of data, need dynamically. there plenty of examples on site. search dynamic pivoting.

edit:

something this:

select * ( select   (case when [fitter] null (select distinct substring([first name],1,1)+' '+[second name] fitters [fitter id]=fitterid) else fitter end) fitter, sum([install sell]) [install sell], sum([install cost]) [install cost], sum([install cost amt]) gross, (select cast(cast((taxstatus/100 * sum([install cost amt])) decimal(18,5)) float)) cis, (select cast(cast((vatstatus/100 * sum([install cost amt])) decimal(18,5)) float)) vat, sum([install cost amt]) - (select cast(cast((taxstatus/100 * sum([install cost amt])) decimal(18,5)) float)) + (select cast(cast((vatstatus/100 * sum([install cost amt])) decimal(18,5)) float)) fitterspay, sum([install cost amt]) + (select cast(cast((vatstatus/100 * sum([install cost amt])) decimal(18,5)) float)) datafile  @temptable group fitterid, fitter, taxstatus, vatstatus) t  unpivot (a b in([install sell],[install cost],[gross]/*,...*/)) pivot (max(a) fitter in([d page],[j hopley]/*,...*/)) p 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -