If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > PIVOT query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-09-12, 13:55
mrpcguy mrpcguy is offline
Registered User
 
Join Date: Mar 2004
Posts: 140
PIVOT query

Need help with PIVOT query. All outputs become zeros

Table look like this:
[c1] [varchar](10) NULL
[c2] [varchar](10) NULL
[c3] [int] NULL
--------------------------------
Contains this:
c1 c2 c3
KATE MILK 5
KATE BEER 5
KATE MILK 5
KATE BEER 5
HANNAH MILK 6
HANNAH BEER 6
HANNAH MILK 6
HANNAH BEER 6
HANNAH MILK 6
--------------------------------
The query:
SELECT C1,
[R1],
[R2],
[R3]
FROM
( SELECT C1,C2,C3
FROM __lab) A
PIVOT ( COUNT(C2)
FOR C2 IN ( [R1],[R2],[R3] )
) B
--------------------------------
Gives this:
C1 R1 R2 R3
KATE 0 0 0
KATE 0 0 0
HANNAH 0 0 0
KATE 0 0 0
HANNAH 0 0 0
KATE 0 0 0
--------------------------------
But what im trying to get is this: (example numbers only)
HANNAH KATE
BEER 30 20
MILK 20 30

I want the output to show how many times HANNAH and KATE have BEER vs MILK in all rows. Just ignore c3
How can i get the output above with PIVOT function?

Thanks in advance

//Mr
Reply With Quote
  #2 (permalink)  
Old 02-09-12, 17:28
Mark_Otmarich Mark_Otmarich is offline
Registered User
 
Join Date: Feb 2012
Posts: 7
You'll need to flip over the pivot
so you've got...

Code:
SELECT C1,
[R1],
[R2],
[R3]
FROM 
( SELECT C1,C2,C3
FROM __lab) A
PIVOT ( COUNT(C2) 
FOR C2 IN ( [R1],[R2],[R3] ) 
) B
Lets change it to...

Code:
SELECT C2, [MILK], [BEER]
FROM (SELECT C2,C1,C3
FROM __lab) A
PIVOT ( COUNT(C2) 
FOR C1 IN ( [MILK], [BEER]) 
) B
So C2 (Drink) is now your first column, pivot columns are now populated from C1 (name).
Reply With Quote
  #3 (permalink)  
Old 02-10-12, 00:55
mrpcguy mrpcguy is offline
Registered User
 
Join Date: Mar 2004
Posts: 140
thanks for your replay, unfournately it dosen't work.
It seems like the C2 is an invalid column in "SELECT C2, [MILK], [BEER]"

If i change the "PIVOT ( COUNT(c2)" to "PIVOT ( COUNT(c3)" it works but still give me zeros
Reply With Quote
  #4 (permalink)  
Old 02-10-12, 01:05
Mark_Otmarich Mark_Otmarich is offline
Registered User
 
Join Date: Feb 2012
Posts: 7
Try

Code:
SELECT *
FROM (SELECT C2,C1,C3
FROM __lab) A

PIVOT 

( SUM(C2) 
FOR C1 IN ( [MILK], [BEER]) 
) AS B
Reply With Quote
  #5 (permalink)  
Old 02-10-12, 01:15
mrpcguy mrpcguy is offline
Registered User
 
Join Date: Mar 2004
Posts: 140
sorry, can't SUM varchar columns.

"Operand data type varchar is invalid for sum operator."
Reply With Quote
  #6 (permalink)  
Old 02-10-12, 01:18
Mark_Otmarich Mark_Otmarich is offline
Registered User
 
Join Date: Feb 2012
Posts: 7
sum(c3) then
Reply With Quote
  #7 (permalink)  
Old 02-10-12, 01:22
mrpcguy mrpcguy is offline
Registered User
 
Join Date: Mar 2004
Posts: 140
yes that works but that's not the result i want.
I want the query to count how many times "milk" and "beer" there is. Not the sum of the c3

thanks for your effort
Reply With Quote
  #8 (permalink)  
Old 02-10-12, 01:39
Mark_Otmarich Mark_Otmarich is offline
Registered User
 
Join Date: Feb 2012
Posts: 7
You've asked for
HANNAH KATE
BEER 30 20
MILK 20 30


What does my query return?

If you do a count(c3) I assume you would get

C1 HANNAH KATE
MILK 3 2
BEER 3 2

Wouldn't sum(c3) give you the result you are after?
Reply With Quote
  #9 (permalink)  
Old 02-10-12, 01:44
mrpcguy mrpcguy is offline
Registered User
 
Join Date: Mar 2004
Posts: 140
since C3 contains more then value of 1 (5 for Kate and 6 for Hannah) the sum would give me the summarize of the value for that column and not a count for each "MILK" and "BEER"

table look like this:

C1, C2, C3
KATE, MILK, 5
KATE, BEER, 5
HANNAH, MILK, 6
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On