Re: apply limit to sum function

From: e-letter <inpost(at)gmail(dot)com>
To: Michael Wood <esiotrot(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: apply limit to sum function
Date: 2012-04-29 19:39:46
Message-ID: CAET1fe6XJR0fqnqDdG8AMrmVXeX7VZ0siKjd3w5i0zQM6RUkpA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On 29/04/2012, Michael Wood <esiotrot(at)gmail(dot)com> wrote:
>
> e.g. do you want to generate all possible permutations of your values
> and then find the sums that are less than your target? If so, how do
> you decide which subset of rows to return?
>

Ideally, yes but that would make solution only via the "knapsack"
combination algorithm type of method?

> i.e.:
>
> 1: (text1, 10) => 10
>
> 2: (text2, 12) => 12
>
> 3: (text3, 23) => 23
>
> 4: (text1, 10), (text2, 12) => 22
>
> 5: (text1, 10), (text3, 23) => 33
>
> 6: (text1, 10), (text2, 12), (text3, 23) => 45
>
> Only options 1 and 2 are < 22, so I assume you're looking for either
> (text1, 10) or (text2, 12) as an answer given this example data? How
> do you choose which one?
>

Would like to be able to choose 'text1' and 'text2' on the basis of
the sum of the values for the tuples being to the target value.

> Or do you just want to take the data in the order given and add the
> values until the total goes above 22 and then return the rows you had
> just before going over the target value?
>

Yes, that would be acceptable: read values in order in the table until
the sum reaches a target value, then return these rows.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Paul . 2012-05-02 21:42:34 PostgresSQL 8.4 - Data output - Newline
Previous Message Michael Wood 2012-04-29 15:35:37 Re: apply limit to sum function