S
S
Sergey2020-01-03 20:27:55
SQL
Sergey, 2020-01-03 20:27:55

How to correctly write a data query in SQL?

There are two tables in the database: primary and secondary sales.

Direct sales to distributor:
period qty net sales
202008 1 90
202009 1 100
202010 3 330
202011 10 1200
202012 0 0

Secondary sales from distributor to end customer:
period qty 202008
0
202009 2
202010 3
202011 9

I need to create a view in which net sales from primary sales are added to secondary sales (the sales price to a distributor is essentially its cost price). At the same time, it should be taken into account that the distributor first sells the products that he purchased in an earlier period (first in - first out). In Excel, I can calculate this with formulas, but I cannot achieve the desired result in sql.
period qty cogs
202008 0 0
202009 2 190
202010 3 330
202011 9 1080
202012 1 120

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question