Re: Sum of multiplied deltas

From: Oliver Kohll - Mailing Lists <oliver(dot)lists(at)gtwm(dot)co(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Sum of multiplied deltas
Date: 2009-06-08 19:04:16
Message-ID: 28E9499F-8D69-432E-B81E-5E198803918D@gtwm.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 8 Jun 2009, at 19:01, David Fetter wrote:

>> Hello!
>>
>> I've the following data:
>> datetime | val1 | val2
>> time1 | 4 | 40%
>> time2 | 7 | 30%
>> time3 | 12 | 20%
>> ...
>>
>> I'd like to sum up the following:
>>
>> (7-4)*30% + (12-7)*20% + ...
>
> This is best done in 8.4 using Windowing. Sadly, it's an 8.4-only
> feature, and dodgy hacks are the rule until you can use them.
>
> Cheers,
> David.
>

I do this type of thing now and again using a self join with an offset.

select test_a.val1 - test_b.val1
from test test_a inner join test test_b
on test_a.pkey = test_b.pkey - 1;

Thought I was quite clever the first time, didn't know it was a dodgy
hack! I'm trying to learn more about windowing before 8.4, how would
this example be done with that?

Regards
Oliver

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Oliver Kohll - Mailing Lists 2009-06-08 19:08:02 Re: [pgsql-general] Daily digest v1.9081 (14 messages)
Previous Message Ioana Danes 2009-06-08 18:58:40 Re: Duplicate key issue in a transaction block