| From: | Raymond O'Donnell <rod(at)iol(dot)ie> |
|---|---|
| To: | btober(at)computer(dot)org |
| Cc: | Berend Tober <btober(at)broadstripe(dot)net>, pgsql-general(at)postgresql(dot)org |
| Subject: | Re: obtain the difference between successive rows |
| Date: | 2012-10-20 16:10:47 |
| Message-ID: | 5082CD07.10108@iol.ie |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
On 20/10/2012 17:02, Berend Tober wrote:
> Thalis Kalfigkopoulos wrote:
>> SELECT current_meter_reading - lag(current_meter_reading) OVER(ORDER
>> BY current_reading_date) AS kWh_diff, extract('days' FROM
>> current_reading_date - lag(current_reading_date) OVER(ORDER BY
>> current_reading_date)) as num_service_days FROM mytable;
>
> How would you get the previous reading (and perhaps the previous read
> date) to also appear on the same output row? The sample table with the
> subtraction I showed for illustration is literally what is printed on
> the bill ... they are not just presenting the quantity used and the
> number of days, but actually the dates and meter readings used to do the
> arithmetic.
Just include them in the SELECT:
SELECT
lag(current_meter_reading) OVER(ORDER BY current_reading_date) AS
kWh_prev,
current_meter_reading - lag(current_meter_reading) OVER(ORDER BY
current_reading_date) AS kWh_diff,
(...etc...)
Ray.
--
Raymond O'Donnell :: Galway :: Ireland
rod(at)iol(dot)ie
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2012-10-20 16:23:34 | Re: obtain the difference between successive rows |
| Previous Message | Berend Tober | 2012-10-20 16:02:49 | Re: obtain the difference between successive rows |