Home All Groups Group Topic Archive Search About
Author
15 Jul 2006 3:31 AM
rodchar
hey all,

i have a small table that looks like the following:

gender/age
-------------
male     18
female  32
male     40
female  25
female  26
female  30

how can i store or display horizontally like the following:
the count for each gender in respective age categories

            ageCategory1     ageCategory2     ageCategory3   
male             1                        0                      1
female          2                        2                       0

Legend:
20-29 Category1
30-39 Category2
40-49 Category3

Is this even possible?

thanks,
rodchar

Author
15 Jul 2006 5:20 AM
Tracy McKibben
rodchar wrote:
Show quote
> hey all,
>
> i have a small table that looks like the following:
>
> gender/age
> -------------
> male     18
> female  32
> male     40
> female  25
> female  26
> female  30
>
> how can i store or display horizontally like the following:
> the count for each gender in respective age categories
>
>             ageCategory1     ageCategory2     ageCategory3   
> male             1                        0                      1
> female          2                        2                       0
>
> Legend:
> 20-29 Category1
> 30-39 Category2
> 40-49 Category3
>
> Is this even possible?
>
> thanks,
> rodchar

This is called a "crosstab" query, and there have been numerous examples
posted to this group in the past week or so.


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
15 Jul 2006 5:23 AM
Razvan Socol
Hello

Try something like this:

SELECT gender,
  SUM(CASE WHEN age BETWEEN 20 AND 29 THEN 1 ELSE 0 END) AS
ageCategory1,
  SUM(CASE WHEN age BETWEEN 30 AND 39 THEN 1 ELSE 0 END) AS
ageCategory2,
  SUM(CASE WHEN age BETWEEN 40 AND 49 THEN 1 ELSE 0 END) AS
ageCategory3
FROM smalltable
GROUP BY gender

Razvan
Author
15 Jul 2006 6:33 PM
rodchar
is there, by any chance, to get the vertical totals at the same time?

Show quote
"Razvan Socol" wrote:

> Hello
>
> Try something like this:
>
> SELECT gender,
>   SUM(CASE WHEN age BETWEEN 20 AND 29 THEN 1 ELSE 0 END) AS
> ageCategory1,
>   SUM(CASE WHEN age BETWEEN 30 AND 39 THEN 1 ELSE 0 END) AS
> ageCategory2,
>   SUM(CASE WHEN age BETWEEN 40 AND 49 THEN 1 ELSE 0 END) AS
> ageCategory3
> FROM smalltable
> GROUP BY gender
>
> Razvan
>
>
Author
16 Jul 2006 6:27 AM
Razvan Socol
rodchar wrote:
> is there, by any chance, to get the vertical totals at the same time?

I guess that you want something like this:
           ageCategory1     ageCategory2     ageCategory3
male             1                   0                 1
female           2                   2                 0
total            3                   2                 1

This would be much easier done in your report designer (in the
front-end).
However if you insist to do it in the back-end, you can do something
like this:

SELECT gender,
  SUM(CASE WHEN age BETWEEN 20 AND 29 THEN 1 ELSE 0 END) AS
ageCategory1,
  SUM(CASE WHEN age BETWEEN 30 AND 39 THEN 1 ELSE 0 END) AS
ageCategory2,
  SUM(CASE WHEN age BETWEEN 40 AND 49 THEN 1 ELSE 0 END) AS
ageCategory3
FROM smalltable
GROUP BY gender
UNION ALL
SELECT 'total' as gender,
  SUM(CASE WHEN age BETWEEN 20 AND 29 THEN 1 ELSE 0 END) AS
ageCategory1,
  SUM(CASE WHEN age BETWEEN 30 AND 39 THEN 1 ELSE 0 END) AS
ageCategory2,
  SUM(CASE WHEN age BETWEEN 40 AND 49 THEN 1 ELSE 0 END) AS
ageCategory3
FROM smalltable

Razvan
Author
15 Jul 2006 5:58 AM
Arnie Rowland
Here is a couple of resources for that tast.

Dynamic Cross-Tabs/Pivot Tables
http://www.sqlteam.com/item.asp?ItemID=2955


How to rotate a table in SQL
Serverhttp://support.microsoft.com/default.aspx?scid=kb;en-us;175574




--
Arnie Rowland
"To be successful, your heart must accompany your knowledge."



Show quote
"rodchar" <rodc***@discussions.microsoft.com> wrote in message
news:0E031EC5-C961-4E71-91F9-AC44D55C7C31@microsoft.com...
> hey all,
>
> i have a small table that looks like the following:
>
> gender/age
> -------------
> male     18
> female  32
> male     40
> female  25
> female  26
> female  30
>
> how can i store or display horizontally like the following:
> the count for each gender in respective age categories
>
>            ageCategory1     ageCategory2     ageCategory3
> male             1                        0                      1
> female          2                        2                       0
>
> Legend:
> 20-29 Category1
> 30-39 Category2
> 40-49 Category3
>
> Is this even possible?
>
> thanks,
> rodchar
Author
15 Jul 2006 7:08 AM
rodchar
thanx everyone for the help.

Show quote
"rodchar" wrote:

> hey all,
>
> i have a small table that looks like the following:
>
> gender/age
> -------------
> male     18
> female  32
> male     40
> female  25
> female  26
> female  30
>
> how can i store or display horizontally like the following:
> the count for each gender in respective age categories
>
>             ageCategory1     ageCategory2     ageCategory3   
> male             1                        0                      1
> female          2                        2                       0
>
> Legend:
> 20-29 Category1
> 30-39 Category2
> 40-49 Category3
>
> Is this even possible?
>
> thanks,
> rodchar

AddThis Social Bookmark Button