Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Aleph_0
Yak Posting Veteran
79 Posts |
Posted - 2012-07-24 : 12:33:07
|
Hi there, it's been a while since I've been stuck on a problem - thanks for all the help in the past! Here's what I'm trying to do:IF OBJECT_ID('tempdb..#AlephTest') IS NOT NULL DROP TABLE #AlephTestSELECT [location_id] = 1 , [customer_id] = 12345 , [age] = 10INTO #AlephTestUNION ALL SELECT 1, 21752, 28UNION ALL SELECT 1, 98712, 25UNION ALL SELECT 1, 12896, 23UNION ALL SELECT 2, 12874, 38UNION ALL SELECT 2, 23987, 29UNION ALL SELECT 2, 23876, 23UNION ALL SELECT 2, 54961, 24SELECT a.location_id , [age_group] = CASE WHEN a.age < 18 THEN '<18' WHEN a.age BETWEEN 18 AND 35 THEN '18 to 35' WHEN a.age > 35 THEN '>35' END , [customer_count] = Count(*)FROM #AlephTest aGROUP BY a.location_id , CASE WHEN a.age < 18 THEN '<18' WHEN a.age BETWEEN 18 AND 35 THEN '18 to 35' WHEN a.age > 35 THEN '>35' ENDORDER BY a.location_id , CASE (CASE WHEN a.age < 18 THEN '<18' WHEN a.age BETWEEN 18 AND 35 THEN '18 to 35' WHEN a.age > 35 THEN '>35' END) WHEN '<18' THEN 1 WHEN '18 to 35' THEN 2 ELSE 3 ENDI'd like all 3 age groups to show for each location, and if there aren't any customers in an age group it should return 0 as the customer count. Is my error in the GROUP BY? I tried putting the Count(*) in a COALESCE(Count(*), 0), but that didn't work.Also, since I'm not an IT guy, would you mind also showing me how a real developer would format my code?Thanks! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-24 : 12:59:31
|
you will need a separate range table like belowCREATE TABLE #AgeRange(RangeDesc varchar(20),AgeStart int,AgeEnd)INSERT #AgeRangeSELECT '<18',1,17 UNION ALLSELECT '18 to 35',18,35 UNION ALLSELECT '>35',36,120 then use it likeSELECT m.location_id,m.RagenDesc,COUNT(n.customer_id) AS CountFROM(SELECT t.location_id,r.RangeDesc,r.AgeStart,r.AgeEndFROM (SELECT DISTINCT location_id FROM #Alephtest) tCROSS JOIN #AgeRange r)mLEFT JOIN #Alephtest nON n.location_id = m.location_idAND n.age BETWEEN m.AgeStart AND m.AgeEndGROUP BY m.location_id,m.RangeDesc ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-24 : 13:23:07
|
| ;with cte1 as(select Startage = 0, endage = 17, description = '<18', seq = 1union allselect Startage = 18, endage = 35, description = '18 to 35', seq = 2union allselect Startage = 36, endage = 1000, description = '>35', seq = 3) ,cte as(select * from cte1cross join (select distinct location_id from #AlephTest) t)SELECT cte.location_id , [age_group] = cte.description , [customer_count] = SUM(case when a.location_id is null then 0 else 1 end)FROM cte left join #AlephTest a on a.age between cte.Startage and cte.endage and a.location_id = cte.location_idGROUP BY cte.location_id , cte.description, cte.seqORDER BY cte.location_id , cte.seq==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Aleph_0
Yak Posting Veteran
79 Posts |
Posted - 2012-07-24 : 13:39:05
|
| Thanks, both of you! I had a feeling I had to create another table, but I had completely forgotten about the CROSS JOIN. You guys are awesome! |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-24 : 13:45:45
|
| To be fair - my post is farly similar to Visakh16's. I wouldn't have posted if I'd read his more carefully. The only thing it adds really is the sequence for the ranges.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Aleph_0
Yak Posting Veteran
79 Posts |
Posted - 2012-07-24 : 13:48:38
|
| I noticed - I took that as confirmation that it was a good approach. Not to mention that is just plain made sense. :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-24 : 14:09:19
|
| If this is something you do frequently report on and if you really want to make solution more scalable then i would suggest making AgeRange as a permanent table. The advantage is anytime you want to regroup your age ranges or add or modify range you can easily do it by means of DML operation to table. Your procedure/sql code will not require any modifications at all.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|