Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

A Cross Tab query, sort of.... Expand / Collapse
Author
Message
Posted Thursday, September 20, 2012 8:16 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, September 8, 2014 4:21 PM
Points: 615, Visits: 373
Hi Experts,

It's Friday afternoon here in NZ and I'm looking at some code, that works fine, thinking "there's got to be a more elegant solution",
but after pulling out what little hair I have left I thought I would use my "ask a friend" option, so....

We have a set of data, where a Customer always has at least one Tariff attached and potentially four. Each Record / Tariff combination is a sperate row in a table. So the data appears like this [we have a little shy of 100,000 records in this table] -

Customer	Tariff	TariffOrder
Record1 121 1
Record1 171 2
Record1 171 3
Record2 121 1
Record2 171 2
Record3 121 1
Record4 101 1
Record5 101 1
Record5 151 2
Record5 171 3
Record5 171 4
Record6 121 1
Record6 171 2
Record7 101 1
Record8 101 1
Record8 171 2
Record8 171 3
Record9 101 1
Record10 171 1


But we have a requirement to report on the distinct combinations, so the report looks like this -

Count	tariff1	tariff2	tariff3	tariff4
3 101 NULL NULL NULL
1 101 151 171 171
1 101 171 171 NULL
1 121 NULL NULL NULL
2 121 171 NULL NULL
1 121 171 171 NULL
1 171 NULL NULL NULL


I've achieved this using a Temp table and a couple of update statements that performs adequately, but is there another solution? Perhaps one that could be flexible with the number of combinations?

We are using SQL 2008 r2.

Thank you for your time!

And some code to create a limited set of sample data -

create table #Cust
(
Customer char(10) not null,
Tariff char(3) not null,
TariffOrder tinyint not null
)

insert into #Cust (Customer, Tariff, TariffOrder) values
('Record1', '121', 1),
('Record1', '171', 2),
('Record1', '171', 3),
('Record2', '121', 1),
('Record2', '171', 2),
('Record3', '121', 1),
('Record4', '101', 1),
('Record5', '101', 1),
('Record5', '151', 2),
('Record5', '171', 3),
('Record5', '171', 4),
('Record6', '121', 1),
('Record6', '171', 2),
('Record7', '101', 1),
('Record8', '101', 1),
('Record8', '171', 2),
('Record8', '171', 3),
('Record9', '101', 1),
('Record10', '171', 1)

Post #1362397
Posted Thursday, September 20, 2012 11:26 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 3,420, Visits: 5,351
I'm no expert, but can I still reply?

What you need is a crosstab query, which is pretty straightforward given a known number of tariffs and the fact that you even supplied a tariff order column.

SELECT Customer, Count=COUNT(*)
,tariff1=MAX(CASE TariffOrder WHEN 1 THEN Tariff END)
,tariff2=MAX(CASE TariffOrder WHEN 2 THEN Tariff END)
,tariff3=MAX(CASE TariffOrder WHEN 3 THEN Tariff END)
,tariff4=MAX(CASE TariffOrder WHEN 4 THEN Tariff END)
FROM #Cust
GROUP BY Customer


Very nice job of posting DDL, sample data and expected results by the way.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1362417
Posted Thursday, September 20, 2012 11:29 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 3,420, Visits: 5,351
Duplicate post deleted.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1362418
Posted Thursday, September 20, 2012 11:32 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 3,420, Visits: 5,351
Duplicate post deleted.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1362420
Posted Sunday, September 23, 2012 1:59 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, September 8, 2014 4:21 PM
Points: 615, Visits: 373
Hi Dwain,

Thanks for your reply, it's not quite what I'm after though.... Your solution appears to provide a count of the Tariffs for each Customer, I need to get a count of the Customers with a particular combination of Tariff. So from the sample data I would expect 7 rows with a count of the various Tariff combinations not 10, being the count of distinct Customers.

Thanks for the post




Post #1363292
Posted Sunday, September 23, 2012 2:28 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 35,269, Visits: 31,762
This will do it. If you want the NULLs to show up instead of the blanks, just remove the ELSE '' from each line where it appears.

WITH 
cteCrossTab AS
(
SELECT Customer,
Tariff1 = MAX(CASE WHEN TariffOrder = 1 THEN Tariff ELSE '' END),
Tariff2 = MAX(CASE WHEN TariffOrder = 2 THEN Tariff ELSE '' END),
Tariff3 = MAX(CASE WHEN TariffOrder = 3 THEN Tariff ELSE '' END),
Tariff4 = MAX(CASE WHEN TariffOrder = 4 THEN Tariff ELSE '' END)
FROM #Cust
GROUP BY Customer
)
SELECT [Count] = COUNT(*),
Tariff1, Tariff2, Tariff3, Tariff4
FROM cteCrossTab
GROUP BY Tariff1, Tariff2, Tariff3, Tariff4
ORDER BY Tariff1, Tariff2, Tariff3, Tariff4
;

Results from the given data in the original post:

Count       Tariff1 Tariff2 Tariff3 Tariff4
----------- ------- ------- ------- -------
3 101
1 101 151 171 171
1 101 171 171
1 121
2 121 171
1 121 171 171
1 171

(7 row(s) affected)


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1363296
Posted Sunday, September 23, 2012 4:31 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, September 8, 2014 4:21 PM
Points: 615, Visits: 373
Jeff,

Thank you for the elegant solution I was after.

Intrigued, I added this code into a stored procedure and compared the performance against the existing, the average over 10 runs indicates that Jeff's solution is approximately twice as fast. Nice one!

Thank you

Post #1363308
Posted Sunday, September 23, 2012 5:10 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 35,269, Visits: 31,762
SeanF-708538 (9/23/2012)
Jeff,

Thank you for the elegant solution I was after.

Intrigued, I added this code into a stored procedure and compared the performance against the existing, the average over 10 runs indicates that Jeff's solution is approximately twice as fast. Nice one!

Thank you



Heh... twice as fast? I'm slipping up in my old age.

Thanks for the feedback, Sean.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1363311
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse