February 12, 2009 at 10:09 am
Hi
I want to take the following two select statements together:
SELECT COUNT(*) AS AM
FROM dwh_test.dim_employees
WHERE
race_code = '01' AND gender_code = '1'
**************************************************
SELECT COUNT(*) AS CM
FROM dwh_test.dim_employees
WHERE
race_code = '04' AND gender_code = '1'
to give me the following result:
AM CM
1234 7809
any ideas???
February 12, 2009 at 10:28 am
it's suprisinglyy easy... you just need to use the two tables as aliased subselects:
SELECT ALIAS1.AM,ALIAS2.CM
FROM
(SELECT COUNT(*) AS AM
FROM dwh_test.dim_employees
WHERE
race_code = '01' AND gender_code = '1'
)ALIAS1,
(SELECT COUNT(*) AS CM
FROM dwh_test.dim_employees
WHERE
race_code = '04' AND gender_code = '1'
)ALIAS2
Lowell
February 12, 2009 at 10:33 am
thank you
February 12, 2009 at 10:39 am
another way to do it wiht a CASE statement, if you need to do lots of different race codes:
SELECT
SUM(CASE WHEN race_code = '01' THEN 1 ELSE 0 END) AS AM ,
SUM(CASE WHEN race_code = '04' THEN 1 ELSE 0 END) AS CM
FROM dwh_test.dim_employees
WHERE gender_code = '1'
Lowell
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply