Re: sql question

From: Joel Burton <jburton(at)scw(dot)org>
To: will trillich <will(at)serensoft(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: sql question
Date: 2001-05-16 20:03:50
Message-ID: Pine.LNX.4.21.0105161600410.22678-100000@olympus.scw.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 16 May 2001, will trillich wrote:

> On Sun, May 13, 2001 at 06:45:05PM +0000, u95886230(at)spawnkill(dot)ip-mobilphone(dot)net wrote:
> > 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
>
> if you're pulling those results into a program (php? perl?) then
> you can do interesting things such as output a break on state
> boundaries to compute state subtotals, and so forth.

You can do it in SQL, with something like:

SELECT accountnum, (SELECT count(*) FROM data a WHERE a.accounttype='a'
and a.accountnum=o.accountnum) AS TotA, .. < same for B and C > .. FROM
data o group by accoutnum;

It won't be terribly fast, but, then, getting all this data into Python or
Perl and doing it there won't be so speedy either.

HTH,
--
Joel Burton <jburton(at)scw(dot)org>
Director of Information Systems, Support Center of Washington

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ryan Mahoney 2001-05-16 20:36:12 Re: Tiiiiiiiiiiiiime
Previous Message User JOHN 2001-05-16 19:51:23 Restore from a dead machine.