I would like to create a custom order in my sql query, just changing one rows position.
This is my current sql results -
Age Category Female Male
-------------------------------
30-39 2772 3193
40-49 1587 2246
50-65 990 3718
Over 65 176 3487
Under 30 1359 1500
I would like them to sort like this, with the 'under 30' at the top -
Age Category Female Male
-------------------------------
Under 30 1359 1500
30-39 2772 3193
40-49 1587 2246
50-65 990 3718
Over 65 176 3487
Here is my code -
SELECT DISTINCT
CASE
WHEN datediff(YYYY,birth_date,getdate()) <= 30 THEN 'Under 30'
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 30 AND 39 THEN '30-39'
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 40 AND 49 THEN '40-49'
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 50 AND 65 THEN '50-65'
WHEN datediff(YYYY,birth_date,getdate()) >= 65 THEN 'Over 65'
END as 'Age Category',
CASE
WHEN datediff(YYYY,birth_date,getdate()) <= 30 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate()) <= 30 and gender ='f' and status ='a' and member_type ='mm')
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 30 AND 39 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate())BETWEEN 30 AND 39 and gender ='f' and status ='a' and member_type ='mm')
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 40 AND 49 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate())BETWEEN 40 AND 49 and gender ='f' and status ='a' and member_type ='mm')
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 50 AND 65 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate())BETWEEN 50 AND 64 and gender ='f' and status ='a' and member_type ='mm')
WHEN datediff(YYYY,birth_date,getdate()) >= 65 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate()) >= 65 and gender ='f' and status ='a' and member_type ='mm')
END as 'Female',
CASE
WHEN datediff(YYYY,birth_date,getdate()) <= 30 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate()) <= 30 and gender ='m' and status ='a' and member_type ='mm')
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 30 AND 39 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate())BETWEEN 30 AND 39 and gender ='m' and status ='a' and member_type ='mm')
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 40 AND 49 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate())BETWEEN 40 AND 49 and gender ='m' and status ='a' and member_type ='mm')
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 50 AND 65 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate())BETWEEN 50 AND 64 and gender ='m' and status ='a' and member_type ='mm')
WHEN datediff(YYYY,birth_date,getdate()) >= 65 THEN (select count(*) from name n1 where datediff(YYYY,n1.birth_date,getdate()) >= 65 and gender ='m' and status ='a' and member_type ='mm')
END as 'Male'
FROM NAME N1
WHERE [STATUS] ='A' AND
MEMBER_TYPE IN ('MM') AND
(
CASE
WHEN datediff(YYYY,birth_date,getdate()) <= 30 THEN 'Under 30'
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 30 AND 39 THEN '30-39'
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 40 AND 49 THEN '40-49'
WHEN datediff(YYYY,birth_date,getdate()) BETWEEN 50 AND 65 THEN '50-65'
WHEN datediff(YYYY,birth_date,getdate()) >= 65 THEN 'Over 65'
END
) IS NOT NULL
group by datediff(YYYY,birth_date,getdate()), member_type
Much appreciated