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 |
fwdtech
Starting Member
11 Posts |
Posted - 2013-08-30 : 14:49:16
|
I have one query that returns a set of age ranges along with the count in that range via:WITH x AS ( select ... ) SELECT CASE WHEN x.age BETWEEN 30 AND 39 THEN '30-39' [ect through 99+] AS [Age], COUNT(*) AS [Count] FROM x GROUP BY CASE WHEN WHEN x.age BETWEEN 30 AND 39 THEN '30-39' [ect through 99+] END;So I get back 30-39 | 15, 40-49 122 (ect...)And another report that generates the average income by client classification via:select x.cl, count(*) as records, sum(w.a, w.b, w.c)from clients cinner join wages w on [bla bla]inner join classifications x on [bla bla]group by x.cindex, x.clWhich gives me "aaa,7,51484.00 | btr,22,85616.00" (ect).Now I'm tasked with generating the age breakdown query combined with the average income by class so I would print out "30-39 = 41566" - "40-49 61523" ect.I tried adding the sum() to the first report but I keep getting the 'group by' error in the query. It's 3 tables, one with wages as 7 separate fields, one with the clients class (int) and one with the classifications themselves (int+description).Can I do the select x with ( select... ) query as I did in the age report and combine it with SUM of values from another query? As of now, I wrote a routine to perform the calculations in .net but I'm curious if I can move this back to SQL.Thanks. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-08-30 : 16:40:14
|
[code]SELECT CAST(Number / 10 AS VARCHAR(3)) + '-' + CAST(Number / 10 + 9 AS VARCHAR(3)), COUNT(*)FROM master.dbo.spt_valuesWHERE Type = 'P'GROUP BY Number / 10;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2013-09-03 : 12:47:17
|
Here's an example on how to chain CTEs and combine them:WITH cteNumbersAS(SELECT NumberFROM master.dbo.spt_valuesWHERE Type = 'P')--SELECT * FROM cteNumbers/* ----------------------------------Number01234...102210231024...*/ ----------------------------------, cteAgeRangeAS(SELECT Number*10 AS RangeBegin, Number*10+9 AS RangeEnd, CAST(Number*10 AS VARCHAR(3)) + '-' + CAST(Number*10 + 9 AS VARCHAR(3)) as AgeRangeFROM cteNumbers AS nWHERE n.Number <= 13 -- Max Age of 139) -- SELECT * FROM cteAgeRange/* ----------------------------------RangeBegin RangeEnd AgeRange0 9 0-910 19 10-1920 29 20-2930 39 30-3940 49 40-4950 59 50-5960 69 60-6970 79 70-7980 89 80-8990 99 90-99100 109 100-109110 119 110-119120 129 120-129130 139 130-139*/ ----------------------------------SELECT AgeRange, COUNT(*) AS TotalNumbersInRangeFROM cteAgeRange AS rJOIN cteNumbers as nON n.Number BETWEEN r.RangeBegin AND r.RangeEndGROUP BY AgeRange,RangeBeginORDER BY RangeBegin/* ----------------------------------AgeRange TotalNumbersInRange0-9 1010-19 1020-29 1030-39 1040-49 1050-59 1060-69 1070-79 1080-89 1090-99 10100-109 10110-119 10120-129 10130-139 10*/ ---------------------------------- |
|
|
|
|
|
|
|