Age Group Sex Year counts1 Year counts 2 Year counts 3
62 Senior M 15 20
29
69 Senior F 21 23
25
My year counts1, Year Counts 2, Year counts 3 results are coming from
three different tables (a,b,c)
Select age, group, year counts1 = select count(*) from a, year
counts2=select count(*) from b, year counts3=select count(*) from c.
From..............................................................
My real life counts for a,b,c are huge in millions and involves joins
with several other tables. Is there a better way to write an efficient
SQL to get these results.
--
Thanks
Michael Coles
SQL Server MVP
Author, "Expert SQL Server 2008 Encryption"
(http://www.apress.com/book/view/1430224649)
----------------
"SQL" <texas...@gmail.com> wrote in message
news:de896812-b3ab-41b5...@j24g2000yqa.googlegroups.com...
This is why we ask for DDL and clear specs.
But here is a guess at improvement. Scalar subquery expressions in a
SELECT list are very expensive, so don't use them.
SELECT *
FROM (SELECT person_age, age_group, sex_code, COUNT(*), 'Table_A'
FROM Table_A
GROUP BY person_age, age_group, sex_code
UNION ALL
SELECT person_age, age_group, sex_code, COUNT(*), 'Table_B'
FROM Table_B
GROUP BY person_age, age_group, sex_code
UNION ALL
SELECT person_age, age_group, sex_code, COUNT(*), 'Table_C'
FROM Table_A
GROUP BY person_age, age_group, sex_code)
AS Report (person_age, age_group, sex_code, group_cnt, data_source);
Trying to format your display in the DB side of the system is always a
mistake. Front end tools will do a much better job. You need to
quite using the old proprietary "<column name> = <expression>" syntax
and start using the Standard "<expression> AS <column name>"
instead.
Does the original data have the bitrthdates or the age as shown? Why
not calculate the age groups with a CASE expression and save a few
disk reads? Look up the ISO sex codes for future reference.
That is all I have without more information.
--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net
"SQL" <texas...@gmail.com> wrote in message
news:de896812-b3ab-41b5...@j24g2000yqa.googlegroups.com...