harsha547
4/2/2017 - 5:52 PM

Display last month sales simultaneously with current month sales in SQL

Display last month sales simultaneously with current month sales in SQL

-- Display last month sales simultaneously with current month sales 

CREATE TABLE #saless
(
ID INT,
Month VARCHAR(30),
Sales INT
);


INSERT INTO #saless VALUES(1, 'Jan',250);

INSERT INTO #saless VALUES(2, 'Feb',350);

INSERT INTO #saless VALUES(3, 'Mar',450);

INSERT INTO #saless VALUES(4, 'Apr',150);

INSERT INTO #saless VALUES(5, 'May',50);

INSERT INTO #saless VALUES(6, 'Jun',225);

INSERT INTO #saless VALUES(7, 'Jul',115);

INSERT INTO #saless VALUES(8, 'Aug',158);

INSERT INTO #saless VALUES(9, 'Sep',425);

INSERT INTO #saless VALUES(10, 'Oct',524);

INSERT INTO #saless VALUES(11, 'Nov',421);

INSERT INTO #saless VALUES(12, 'Dec',620);

SELECT * ,  
		LAG(Sales,1) OVER(ORDER BY ID ASC) As  last_month_sales

	FROM #saless ORDER BY ID ASC;