Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH T AS (
- select
- person_id,
- age = dbo.fn_Age(cp.birth_date,GETDATE())
- from core_person cp
- WHERE
- birth_date != '1900-01-01 00:00:00.000'
- and gender != -1 and gender !=2
- and member_status in (958)
- and record_status=0 ) ,
- Age_Groups AS (
- select ' 0-5' [Age Group] , count(person_id) C
- from T
- where age < 6
- union
- select ' 6-17' [Age Group] , count(person_id) C
- from T
- where age >= 6 and age <18
- union
- select '18-24' [Age Group] , count(person_id) C
- from T
- where age >= 18 and age <25
- union
- select '25-34' [Age Group] , count(person_id) C
- from T
- where age >= 25 and age <35
- union
- select '35-49' [Age Group] , count(person_id) C
- from T
- where age >= 35 and age <50
- union
- select '50-64' [Age Group] , count(person_id) C
- from T
- where age >= 50 and age <65
- union
- select '65-79' [Age Group] , count(person_id) C
- from T
- where age >= 65 and age <80
- union
- select '80+' [Age Group] , count(person_id) C
- from T
- where age >= 80
- union
- select 'Total' , count(person_id) C
- from T
- )
- SELECT
- G.[Age Group],
- [Count] = G.C,
- '%' = convert(VARCHAR,convert(MONEY,100.0 * G.C / (SELECT C FROM Age_Groups G WHERE G.[Age Group] = 'Total'),1)) + '%'
- FROM Age_Groups G
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement