Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What am I doing wrong with this pivot?

Status
Not open for further replies.

ifthenelsenull

Technical User
Nov 17, 2011
31
US
I am pivoting a temp table and the table is fine with no nulls or additional values but when I pivot the data gets split in half and I end up with nulls where there should be values. Instead of 1 row with 20 columns I get two rows with 20 columns with half the values in the top row and half in the bottom row.

The pivot looks like :
<code>

SELECT 'TOP_VALUE' AS New_VALUE,
[Wnd],
[ Type1 ],
[ Type8 ],
[ Type7 ],
[ Type6 ],
[ Type5 ],
[ Type4 ],
[ Type3 ],
[ Type2 ],
[ Stg1 ],
[ Stg2 ],
[ Stg3 ],
[ Stg4 ],
[ Stg5 ],
[ Stg6 ],
[ Stg7 ],
[ Stg8 ],
[Score ],
[SknRisk ]
FROM
(SELECT *
FROM #tbla0data
) AS SourceTable

PIVOT
(
MIN(VALUE)
FOR findingabbr IN (
[wnd],
[ Type1 ],
[ Type8 ],
[ Type7 ],
[ Type6 ],
[ Type5 ],
[ Type4 ],
[ Type3 ],
[ Type2 ],
[ Stg1 ],
[ Stg2 ],
[ Stg3 ],
[ Stg4 ],
[ Stg5 ],
[ Stg6 ],
[ Stg7 ],
[ Stg8 ],
[A_BradenScore ],
[D_N_SknRisk ])
) AS PivotTable;


-- FIRST ATTEMPT I thought this might just be missing something
--select DISTINCT * from #tbla0data
--pivot (MAX(value) for findingabbr IN(
--[D_N_noWnd],
--[ Type1 ],
--[ Type8 ],
--[ Type7 ],
--[ Type6 ],
--[ Type5 ],
--[ Type4 ],
--[ Type3 ],
--[ Type2 ],
--[ Stg1 ],
--[ Stg2 ],
--[ Stg3 ],
--[ Stg4 ],
--[ Stg5 ],
--[ Stg6 ],
--[ Stg7 ],
--[ Stg8 ],
--[Score ],
--[Risk ])) as maxvalue

</code>
 
I received an answer and basically I needed to pivot by the fields with my data not *.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top