Advertisement
DataCCIW

Membership Age Percentages for RGFQ

Nov 25th, 2019
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.17 KB | None | 0 0
  1. WITH T AS (
  2.  
  3. select
  4. person_id,
  5. age = dbo.fn_Age(cp.birth_date,GETDATE())
  6. from core_person cp
  7. WHERE
  8. birth_date != '1900-01-01 00:00:00.000'
  9. and gender != -1 and gender !=2
  10. and member_status in (958)
  11. and record_status=0 ) ,
  12.  
  13. Age_Groups AS (
  14. select ' 0-5' [Age Group] , count(person_id) C
  15. from T
  16. where age < 6
  17. union
  18. select ' 6-17' [Age Group] , count(person_id) C
  19. from T
  20. where age >= 6 and age <18
  21. union
  22. select '18-24' [Age Group] , count(person_id) C
  23. from T
  24. where age >= 18 and age <25
  25. union
  26. select '25-34' [Age Group] , count(person_id) C
  27. from T
  28. where age >= 25 and age <35
  29. union
  30. select '35-49' [Age Group] , count(person_id) C
  31. from T
  32. where age >= 35 and age <50
  33. union
  34. select '50-64' [Age Group] , count(person_id) C
  35. from T
  36. where age >= 50 and age <65
  37. union
  38. select '65-79' [Age Group] , count(person_id) C
  39. from T
  40. where age >= 65 and age <80
  41. union
  42. select '80+' [Age Group] , count(person_id) C
  43. from T
  44. where age >= 80
  45. union
  46. select 'Total' , count(person_id) C
  47. from T
  48. )
  49.  
  50. SELECT
  51.    G.[Age Group],
  52.    [Count] = G.C,
  53.    '%' = convert(VARCHAR,convert(MONEY,100.0 * G.C / (SELECT C FROM Age_Groups G WHERE G.[Age Group] = 'Total'),1)) + '%'
  54. FROM Age_Groups G
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement