Re: sql question

From: "Blackin" <rblackin(at)home(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: sql question
Date: 2001-05-21 00:44:24
Message-ID: IxZN6.111959$K5.10632146@news1.rdc1.nj.home.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

select sum(case when ordertype='A' then 1 else 0 end) as TotA,
sum(case when ordertype='B' then 1 else 0 end) as TotB,
sum(case when ordertype='C' then 1 else 0 end) as TotC
>From Thetable
where Date="May"

This works if you know how many columns(OrderTypes) that you have. If you
do not know this query can be built dynamically.

Rob

<u95886230(at)spawnkill(dot)ip-mobilphone(dot)net> wrote in message
news:l(dot)989779505(dot)1960693359(at)[212(dot)171(dot)170(dot)132](dot)(dot)(dot)
> Please help, I am trying to write an SQL statement but with no success as
I am just
> starting out with sql.
>
> I have a table with 3 columns: Account# ,OrderType and date
> example of data:
> Account# &brvbar; Ordertype &brvbar; Date
> 1 &brvbar; A &brvbar; April
> 1 &brvbar; B &brvbar; May
> 1 &brvbar; B &brvbar; May
> 2 &brvbar; B &brvbar; April
> 2 &brvbar; B &brvbar; May
> 2 &brvbar; C &brvbar; May
> 3 &brvbar; C &brvbar; May
>
>
> I need to write a select that will show me the totals of EACH type for
EACH account AND
> total ordersplaced for a SPECIFIC month eg..Show me the results for May...
>
> account &brvbar; TotA &brvbar; TotB &brvbar; TotC &brvbar; Total
> 1 &brvbar; 0 &brvbar; 2 &brvbar; 0 &brvbar; 2
> 2 &brvbar; 0 &brvbar; 1 &brvbar; 1 &brvbar; 2
> 3 &brvbar; 0 &brvbar; 0 &brvbar; 1 &brvbar; 1
>
> I can use temp tables, but need a solution written as basic as pssible so
I can understand
> it (all in the form select this from that)
> any help would be fantastic as I am completely stuck and have been trying
for about a week
>
> thanks
> sgebbie(at)ciaoweb(dot)it
>
>
>
>
> --
> Sent by sgebbie from ciaoweb piece from it
> This is a spam protected message. Please answer with reference header.
> Posted via http://www.usenet-replayer.com/cgi/content/new

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Lamar Owen 2001-05-21 02:43:48 Re: Compiling for Caldera on 2nd Drive
Previous Message Johnny_5555 2001-05-20 23:27:48 Simple question for SQL Student