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 | CodeNorth | John | ANorth | John | ANorth | John | BNorth | Tom | ANorth | Tom | CWest | John | BWest | John | AWest | John | BWest | Tom | AI 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)aPIVOT ( MAX(n) FOR code IN ([a],[b],[c]))P |
 |
|
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)aPIVOT ( 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. |
 |
|
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'.' |
 |
|
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)aPIVOT ( 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 nameie like belowSELECT 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)aPIVOT ( MAX(n) FOR code IN ([a],[b],[c]))P ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|