Wednesday, 16 September 2015

To date calculations in SQL

CREATE TABLE m2dt(sale_amt INT, sale_dt DATE);
INSERT INTO m2dt VALUES(30,'2015-01-01'),(50,'2015-02-01'),(100,'2016-01-01'),(50,'2016-01-10'),(30,'2016-01-03');

Month to Date calculation:
SELECT 
SALE_DT,
SUM(sale_amt) OVER (PARTITION BY TRUNC(sale_dt,'MM') ORDER BY STRING(sale_dt)) AS amt 
FROM m2dt;

Year to Date calculation:
SELECT 
SALE_DT,
SUM(sale_amt) OVER (PARTITION BY YEAR(sale_dt) ORDER BY TRUNC(sale_dt,'MM')) AS amt 

FROM m2dt;


No comments:

Post a Comment

 A good reference for Shell scripting  https://linuxcommand.org/lc3_writing_shell_scripts.php