From: | "Muhyiddin A(dot)M Hayat" <middink(at)indo(dot)net(dot)id> |
---|---|
To: | <pgsql-novice(at)postgresql(dot)org> |
Cc: | "Alexander Pucher" <pucher(at)atlas(dot)gis(dot)univie(dot)ac(dot)at> |
Subject: | Re: Cross-classified table |
Date: | 2003-10-21 11:41:59 |
Message-ID: | 00c201c397c8$57120d00$1f00a8c0@middinkcomp |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
> Hi,
> I'm looking for the correct syntax of getting a cross-classified table
> from my SELECT statement.
>
> Let's pretend I have this table:
>
>
> origin sex
> ----------------
> USA male
> USA female
> China male
> China male
> UK male
> USA male
>
>
> and I want as result something like:
>
> male female
>
> USA 2 1
> China 2 0
> UK 1 0
>
>
> How can I get this?
>
You can use below SQL
SELECT
origin,
SUM(
CASE
WHEN (sex ='male') THEN 1
ELSE 0
END
) AS male,
SUM(
CASE WHEN (sex ='female') THEN 1
ELSE 0
END
) AS female
FROM
table1
GROUP BY origin
Best regards,
Muhyiddin A.M Hayat
From | Date | Subject | |
---|---|---|---|
Next Message | Michael Glaesmann | 2003-10-21 12:02:46 | Custom function problems |
Previous Message | Bruno LEVEQUE | 2003-10-20 19:53:09 | Re: Cross-classified table |