SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pivot Query


Pivot Query

Author
Message
:O)
:O)
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 32
I just don't get it w00t

Here's some sample data:-


create table SalesFigures
(Id int, SalesMan varchar(20), Product varchar(20), NbrSales int, SalesValue money)

insert into SalesFigures values(1, 'George', 'WidgetA', 12, 120.00)
insert into SalesFigures values(2, 'Paul', 'WidgetA', 5, 50.00)
insert into SalesFigures values(3, 'Bill', 'WidgetA', 1, 10.00)
insert into SalesFigures values(4, 'Bill', 'WidgetB', 1, 100.00)
insert into SalesFigures values(5, 'John', 'WidgetA', 25, 250.00)
insert into SalesFigures values(6, 'Ringo','WidgetB', 1, 100.00)
insert into SalesFigures values(7, 'Paul', 'WidgetB', 10, 1000.00)
insert into SalesFigures values(8, 'George','WidgetB', 1, 100.00)
insert into SalesFigures values(9, 'George','WidgetB', 1, 100.00)
insert into SalesFigures values(10, 'Ringo','WidgetA', 1, 10.00)
insert into SalesFigures values(11, 'Ringo','WidgetA', 2, 2.00)
insert into SalesFigures values(12, 'George','WidgetB', 3, 300.00)



Using a sql2000 approach this would be my query :-

Select
SalesMan,
Sum(Case When Product = 'WidgetA' Then NbrSales End) As 'WidgetA Sales',
Sum(Case When Product = 'WidgetA' Then SalesValue End) As 'WidgetA Value',
Sum(Case When Product = 'WidgetB' Then NbrSales End) As 'WidgetB Sales',
Sum(Case When Product = 'WidgetB' Then SalesValue End) As 'WidgetB Value'
From
SalesFigures
Group By
Salesman



This gives a nice summary of salesman's performance. that looks like:-


SalesMan WidgetA Value WidgetB Value
Bill 1 10.00 1 100.00
George 12 120.00 5 500.00
John 25 250.00 NULL NULL
Paul 5 50.00 10 1000.00
Ringo 3 12.00 1 100.00


Sorry about the formatting, it seems to be hard to post a results grid in a sensible manner. Anyway, the question is, how would I achieve the same result using PIVOT?

Thanks.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16756 Visits: 19120
I don't get it. What are your expected results?


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
:O)
:O)
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 32
If the point of PIVOT is to help turn row based values into column based values, how do I use it to produce the same results as the approach using the CASE statements?

I'm just struggling to see the point of PIVOT - the syntax is absolutely horrible and confusing.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16756 Visits: 19120
:O) (12/31/2012)
If the point of PIVOT is to help turn row based values into column based values, how do I use it to produce the same results as the approach using the CASE statements?

I'm just struggling to see the point of PIVOT - the syntax is absolutely horrible and confusing.


I agree with you, I don't like the PIVOT clause either.
This article can help you to clarify a little bit the differences between the two methods.Cross Tabs & PIVOT


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search