Re: How do write a query...

From: Mischa Sandberg <mischa(dot)sandberg(at)telus(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: How do write a query...
Date: 2005-06-11 03:35:44
Message-ID: 1118460944.42aa5c1012c59@webmail.telus.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> |From: Alain Reymond [mailto:arwebmail(at)skynet(dot)be]
> |Sent: Mittwoch, 01. Juni 2005 18:01
> |Subject: [SQL] How do write a query...
> |
> |I have a table like
> |Id Num Date AValue
> |1 10 01/01/2005 50
> |2 10 31/05/2005 60
> |3 25 02/02/2005 55
> |4 25 15/03/2005 43
> |5 25 28/05/2005 62
> |etc..
> |
> |Id is unique, Num is an identification number with duplicates
> possible,
> |date is a ... date and Avalue... a value!
> |
> |If we have
> |Id Num Date AValue
> |Id1 Num1 Date1 AValue1
> |Id2 Num1 Date2 AValue2
> |
> |The table is ordered on Num+Date.
> |What I would like to calculate is (AValue2-AValue1) for a given Num
> |(here num1).
> |
> |In this case, I would have to calculate
> |60-50 for Num 10
> |and
> |43-55, 62-43 for Num 25.

Not sure if this covers all that you want, given your examples (what if
there's only one row for a given Num value?), but ...

select Num, AValue-AValue1
from Tafel
join (select Num, min(AValue) as AValue1
from Tafel group by Num) as T using(Num)
where AValue > AValue1

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Larry Meadors 2005-06-12 04:23:11 Looking for info on 8.1 features, and some examples
Previous Message Tino Wildenhain 2005-06-10 11:10:27 Re: [SQL] Permission denied for language pltclu