Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get a count under headaings

Author  Topic 

Cordo
Starting Member

1 Post

Posted - 2011-12-08 : 05:35:39
Hi Guys,

How do I write code to split out

Say I have a column with names in it and a column with fruit types. These are the fruit types. Apple, Orange and Banana. A person will eat many of these fruits and I need to pull back the count of each fruit eaten by each person.

Basically I want my result to look like this:

Name Apple Orange Banana

Adam 4 2 7
Barry 3 5 8
Cody 6 3 7



Is there anyway to write code to do this?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-12-08 : 05:48:17
Have a look at PIVOT in SQL Server help file. Also refer this
http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -