Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 A Cross Tab query, sort of.... Rate Topic Display Mode Topic Options
Author
 Message
 Posted Thursday, September 20, 2012 8:16 PM
 Say Hey Kid Group: General Forum Members Last Login: Tuesday, September 27, 2016 9:17 PM Points: 703, Visits: 420
 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 TariffOrderRecord1 121 1Record1 171 2Record1 171 3Record2 121 1Record2 171 2Record3 121 1Record4 101 1Record5 101 1Record5 151 2Record5 171 3Record5 171 4Record6 121 1Record6 171 2Record7 101 1Record8 101 1Record8 171 2Record8 171 3Record9 101 1Record10 171 1`But we have a requirement to report on the distinct combinations, so the report looks like this -`Count tariff1 tariff2 tariff3 tariff43 101 NULL NULL NULL1 101 151 171 1711 101 171 171 NULL1 121 NULL NULL NULL2 121 171 NULL NULL1 121 171 171 NULL1 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 Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 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 #CustGROUP 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!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1362417
 Posted Thursday, September 20, 2012 11:29 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 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!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1362418
 Posted Thursday, September 20, 2012 11:32 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 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!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1362420
 Posted Sunday, September 23, 2012 1:59 PM
 Say Hey Kid Group: General Forum Members Last Login: Tuesday, September 27, 2016 9:17 PM Points: 703, Visits: 420
 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-Forever Group: General Forum Members Last Login: Today @ 3:23 PM Points: 42,036, Visits: 39,415
 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 1711 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." Helpful Links:How to post code problemsHow to post performance problems
Post #1363296
 Posted Sunday, September 23, 2012 4:31 PM
 Say Hey Kid Group: General Forum Members Last Login: Tuesday, September 27, 2016 9:17 PM Points: 703, Visits: 420
 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-Forever Group: General Forum Members Last Login: Today @ 3:23 PM Points: 42,036, Visits: 39,415
 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." Helpful Links:How to post code problemsHow to post performance problems
Post #1363311

 Permissions