pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ragnar Hafstaư <gnari@simnet.is>
To: Michael L. Hostbaek <mich@the-lab.org>
Cc: pgsql-sql@postgresql.org
Subject: Re: select & group by
Date: Mon, 04 Apr 2005 10:47:10 +0000
Message-ID: <1112611630.19933.6.camel@localhost.localdomain> (raw)
In-Reply-To: <20050404101647.GJ23648@mich2.itxmarket.com>
References: <20050404101647.GJ23648@mich2.itxmarket.com>

On Mon, 2005-04-04 at 12:16 +0200, Michael L. Hostbaek wrote:
> I've got a problem selecting some specific data from my table. Imagine
> the following rows:
> 
> part	| mfg	| qty	| price	| eta	
> ---------------------------------------
> TEST1	 ABC	 10	 100		(No ETA, as item is in stock)
> TEST1	 ABC	 12	 120	 04/04
> TEST2	 CBA	 17	 10	 05/05
> TEST2	 CBA	 10	 20		(No ETA, as item is in stock)
>
> I'd like my selection to produce the following result:
> 
> part	| mfg	| qty	| qty incoming	| highest price	| eta
> -------------------------------------------------------------
> TEST1	 ABC	 10	 12		 120		 04/04
> TEST2	 CBA	 10	 17		 20		 05/05
> 
> Any clues on how to do this ? I kow the group by part, mfg, max(price) -
> but I do not know how to deal with the splitting up qty and stock qty
> and incoming qty.

use CASE. for example, something like:

select part,mfg,
       sum(CASE WHEN eta is NULL then qty ELSE 0 END) as qty,
       sum(CASE WHEN eta is NULL then 0 ELSE qty END) as "qty incoming",
       max(price) as "highest price",
       min(eta) as eta
group by part,mfg;

gnari





view thread (5+ messages)  latest in thread

Message-ID: <1112611630.19933.6.camel@localhost.localdomain>
Permalink:  ../1112611630.19933.6.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1112611630.19933.6.camel@localhost.localdomain

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: gnari@simnet.is, mich@the-lab.org
  Subject: Re: select & group by
  In-Reply-To: <1112611630.19933.6.camel@localhost.localdomain>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox