Re: How to distribute quantity if same product is in multiple rows
Вложения
В списке pgsql-general по дате отправления:
| От | Tim Landscheidt |
|---|---|
| Тема | Re: How to distribute quantity if same product is in multiple rows |
| Дата | |
| Msg-id | m3wrsql37i.fsf@passepartout.tim-landscheidt.de обсуждение исходный текст |
| Ответ на | How to distribute quantity if same product is in multiple rows ("Andrus" <kobruleht2@hot.ee>) |
| Список | pgsql-general |
(anonymous) wrote:
> Order contains same product in multiple rows.
> I tried to calculate undelivered quantity using script below but it produces
> wrong result:
> delivered quantity is substracted from both rows, not distributed.
> How to distibute undelivered quantity according to row quantity in every row
> ?
> Can it be done using SQL or should SCAN loop in plpgsql used?
> [...]
It can be done in SQL: "SUM(kogus) OVER (PARTITION BY toode
ORDER BY ID) - kogus" (*1) will give you the running sum of
the product up to that row. You can then subtract that value
from the delivered quantity to calculate the delivered quan-
tity for the current row.
But doing so automatically is probably bad. For example,
if a user has a purchase order with one position of two
pieces and one position of four, it is very likely that when
a shipment of four pieces arrives, the latter position shall
be marked as delivered. So I would leave the decision to the
user.
Tim
(*1) In PostgreSQL 9.0, you might be able to use "ROWS
BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING" instead
(untested).
В списке pgsql-general по дате отправления:
Сайт использует файлы cookie для корректной работы и повышения удобства. Нажимая кнопку «Принять» или продолжая пользоваться сайтом, вы соглашаетесь на их использование в соответствии с Политикой в отношении обработки cookie ООО «ППГ», в том числе на передачу данных из файлов cookie сторонним статистическим и рекламным службам. Вы можете управлять настройками cookie через параметры вашего браузера