Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get the amount in two days

I’m trying to write a query that will return the amount of sales given the previous day. I am doing a test task for an internship device but have not done this before.

Source table:

saledate salesum
2022-01-01 100
2022-01-02 150
2022-01-03 200
2022-01-05 100

Estimated result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

saledate salesum
2022-01-01 100
2022-01-02 250
2022-01-03 350
2022-01-05 300

My query:

SELECT t1.saledate, t1.salesum=t1.salesum+t2.salesum 
FROM sales t1
INNER JOIN (
  SELECT saledate, salesum FROM sales 
) t2 
ON t1.saledate=t2.saledate;

My result:

saledate salesum
2022-01-01 f
2022-01-02 f
2022-01-03 f
2022-01-05 f

>Solution :

select  saledate
       ,salesum + coalesce(lag(salesum) over(order by saledate),0) as salesum
from    t
saledate salesum
2022-01-01 100
2022-01-02 250
2022-01-03 350
2022-01-05 300

Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading