A
A
Alexey Bogomolov2016-02-12 19:22:13
SQL
Alexey Bogomolov, 2016-02-12 19:22:13

How to distribute replenishment among purchases?

There are 2 tables, one contains purchases, the other replenishment. Purchases are indivisible, replenishments are divisible, data is sorted by date. The task is to find the distribution of replenishment of the user's account by the purchases he made.
Example:
replenishment (id, amount):
a1, 200r
a2, 150r
a3, 300r
purchases (id, amount)
s1, 100r
s2, 200p
s3, 105r
As a result, you need to get (replenishment id, purchase id)
s1, a1
s2, a1
s2, a2
​​s3, a2
s3, a3
Easy to solve if stacks or queues are available, but can it be solved in SQL without using procedural statements?
Upd: one of the options:
A colleague suggested splitting up replenishments and purchases, say, up to 1 ruble, and then combine the samples and group them by goods.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
redakoc, 2016-02-12
@redakoc

Well, for example, a trigger. When inserting data into replenishment or write-off tables, enter the correspondence you need into an auxiliary table.
On a bare select, I would not even get involved. Intuition tells us that even if this is real, it will work very slowly on real data volumes.

A
Alexey Bogomolov, 2016-03-16
@horse315

As a result, they did it in the backend, and not in SQL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question