SQL Server Pivot Table Help -
i'm trying turn following resultset
meetings covers date typename 1 3 2010-10-14 breakfast 1 1 2010-10-14 lunchcooked 2 4 2010-10-15 breakfast 1 3 2010-10-18 breakfast 1 3 2010-10-19 breakfast 1 1 2010-10-19 lunchsandwich 1 3 2010-10-20 breakfast 1 3 2010-10-21 breakfast 1 3 2010-10-22 breakfast
into format following fields
date breakfastmeetings breakfastcovers lunchsandwichmeetings lunchsandwichcovers lunchcookedmeetings lunchcookedcovers
am right in thinking can done pivot tables? pointers great otherwise i'm going end taking sort of hacky temp table route data format.
thanks
here's way it. needs , unpivot , pivot operation. unpivot combines meetings , covers single column , changes typename values desired column names.
the pivot uses results of unpivot provide final format.
select thedate, breakfastmeetings, breakfastcovers, lunchsandwichmeetings, lunchsandwichcovers, lunchcookedmeetings, lunchcookedcovers ( -- unpivot put counts in single column , create new type names select thedate, typename + meetingcover typename, rowcounts (select thedate, typename, meetings, covers mytable ) p unpivot (rowcounts meetingcover in (meetings, covers) ) unpivottable ) source -- pivot unpivoted data create new columns pivot ( max(rowcounts) typename in (breakfastmeetings, breakfastcovers, lunchsandwichmeetings, lunchsandwichcovers, lunchcookedmeetings, lunchcookedcovers) ) pivottable order thedate
Comments
Post a Comment