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 the result I need. Maybe Pivot?

Author  Topic 

ugh3012
Yak Posting Veteran

62 Posts

Posted - 2014-02-04 : 14:10:13



Here is my data set in raw format.


Region | Name | Code
North | John | A
North | John | A
North | John | B
North | Tom | A
North | Tom | C
West | John | B
West | John | A
West | John | B
West | Tom | A


I need to figure out how to get this result using SQL.


Region | Name | A | B | C
North | John | 2 | 1 | 0
North | Tom | 1 | 0 | 1
West | John | 1 | 2 | 0
West | Tom | 1 | 0 | 0


How can I accomplish this using SQL?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-02-04 : 17:03:50
If you know the codes that you are going to have in advance, you can use the PIVOT operator like shown below. If not, you will need to use dynamic PIVOT (google for Madhivanan's dynamic pivot code)
SELECT region, nam, COALESCE(a,0) AS A, COALESCE(b,0) AS B, COALESCE(c,0) AS C FROM 
(
SELECT region, name, code, COUNT(*) AS N FROM YourTable GROUP BY region, name, code
)a
PIVOT ( MAX(n) FOR code IN ([a],[b],[c]))P
Go to Top of Page

ugh3012
Yak Posting Veteran

62 Posts

Posted - 2014-02-07 : 08:52:46
quote:
Originally posted by James K

If you know the codes that you are going to have in advance, you can use the PIVOT operator like shown below. If not, you will need to use dynamic PIVOT (google for Madhivanan's dynamic pivot code)
SELECT region, nam, COALESCE(a,0) AS A, COALESCE(b,0) AS B, COALESCE(c,0) AS C FROM 
(
SELECT region, name, code, COUNT(*) AS N FROM YourTable GROUP BY region, name, code
)a
PIVOT ( MAX(n) FOR code IN ([a],[b],[c]))P




I m getting this message 'No column name was specified for column 2 of 'a'.'

I am at lost about this issue. What I am doing wrong? When I double click the error, it highlights the row with ')a' right above the word pivot from your example.
Go to Top of Page

ugh3012
Yak Posting Veteran

62 Posts

Posted - 2014-02-10 : 08:52:56
Does any one know what this message mean? 'No column name was specified for column 2 of 'a'.'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-10 : 08:55:08
quote:
Originally posted by ugh3012

quote:
Originally posted by James K

If you know the codes that you are going to have in advance, you can use the PIVOT operator like shown below. If not, you will need to use dynamic PIVOT (google for Madhivanan's dynamic pivot code)
SELECT region, nam, COALESCE(a,0) AS A, COALESCE(b,0) AS B, COALESCE(c,0) AS C FROM 
(
SELECT region, name, code, COUNT(*) AS N FROM YourTable GROUP BY region, name, code
)a
PIVOT ( MAX(n) FOR code IN ([a],[b],[c]))P




I m getting this message 'No column name was specified for column 2 of 'a'.'

I am at lost about this issue. What I am doing wrong? When I double click the error, it highlights the row with ')a' right above the word pivot from your example.


then i'm sure you're using an expression for your second column. in such cases, you need to give it an alias (short name) for derived table to pick it up as column name

ie like below

SELECT region, nam, COALESCE(a,0) AS A, COALESCE(b,0) AS B, COALESCE(c,0) AS C FROM
(
SELECT region, <your expression AS columnname, code, COUNT(*) AS N FROM YourTable GROUP BY region, name, code
)a
PIVOT ( MAX(n) FOR code IN ([a],[b],[c]))P


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -