T-SQL Query | [Finding Previous Value Puzzle] – Write a query which will extract the previous value from the currentQuota for each row.
Input
BusinessEntityID | SalesYear | CurrentQuota |
275 | 2005 | 367000 |
275 | 2005 | 556000 |
275 | 2006 | 502000 |
275 | 2006 | 550000 |
275 | 2006 | 1429000 |
275 | 2006 | 1324000 |
Output ..> We need to extract the last value from the currentQuota ( Last Column )
BusinessEntityID | SalesYear | CurrentQuota | lagCurrentData |
275 | 2005 | 367000 | 0 |
275 | 2005 | 556000 | 367000 |
275 | 2006 | 502000 | 556000 |
275 | 2006 | 550000 | 502000 |
275 | 2006 | 1429000 | 550000 |
275 | 2006 | 1324000 | 1429000 |
Rules/Restrictions
◾The solution should be should use “SELECT” statement or “CTE”.
◾Send your solution to pawankkmr@gmail.com
◾Do not post you solution in comments section
Script Use the below script to generate the source table and fill them up with the sample data.
--So we first create the table CREATE TABLE lag ( BusinessEntityID INT ,SalesYear INT ,CurrentQuota DECIMAL(20,4) ) GO INSERT INTO lag SELECT 275 , 2005 , '367000.00' UNION ALL SELECT 275 , 2005 , '556000.00' UNION ALL SELECT 275 , 2006 , '502000.00' UNION ALL SELECT 275 , 2006 , '550000.00' UNION ALL SELECT 275 , 2006 , '1429000.00' UNION ALL SELECT 275 , 2006 , '1324000.00'
Update May 14 | Solution
-- /************ SOLUTION 1 | Pawan Kumar Khowal ****************/ ;WITH CTE AS ( SELECT BusinessEntityID ,SalesYear ,CurrentQuota , ROW_NUMBER() OVER (ORDER BY BusinessEntityID ) AS ID FROM lag ) SELECT c.BusinessEntityID ,c.SalesYear , c.CurrentQuota , ISNULL((SELECT TOP 1 d.CurrentQuota FROM CTE d WHERE c.ID > d.ID ORDER BY ID DESC ),0) lagCurrentData FROM CTE c /************ SOLUTION 2 | Pawan Kumar Khowal ****************/ ;WITH CTE AS ( SELECT BusinessEntityID ,SalesYear ,CurrentQuota ,ROW_NUMBER()OVER (ORDERBY BusinessEntityID )AS ID FROM lag ) SELECT c.Id ,c.BusinessEntityID ,c.SalesYear , c.CurrentQuota,ISNULL(d.CurrentQuota,0) lagCurrentData FROM CTE c LEFTOUTERJOIN CTE d ON c.ID =(d.ID+1) -- |
Add a comment if you have any other solution in mind. We all need to learn.
Keep Learning
Pingback: Puzzles in Microsoft sql server – uniqueblogforsql
Pingback: uniqueblogforsql
I really enjoy the puzzles on this site, Pawan.
This is perhaps a lazy solution on my part, but I simply used the Lag() window function:
SELECT
BusinessEntityID
, SalesYear
, CurrentQuota
, LAG(CurrentQuota, 1, 0) OVER(ORDER BY CurrentQuota) AS lagCurrentData
FROM Puzzles.lag;
LikeLiked by 2 people
Thank you Cameron !!!
LikeLike
SELECT *,ISNULL(LAG(CurrentQuota) OVER (ORDER BY CurrentQuota),0) as lagCurrentData
FROM lag
LikeLike
SELECT BusinessEntityID,SalesYear,CurrentQuota,LAG(CurrentQuota,1,0) OVER (ORDER BY SalesYear) AS Previous FROM Lag
LikeLike
https://polldaddy.com/js/rating/rating.jshttps://polldaddy.com/js/rating/rating.js;WITH CTE AS
(
SELECT *, ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS Num FROM lag
)
SELECT C.BusinessEntityID, C.SalesYear, C.CurrentQuota,
ISNULL((SELECT CurrentQuota FROM CTE WHERE Num = C.Num-1), 0)
FROM CTE C
LikeLike
select BusinessEntityID, SalesYear, CurrentQuota, lag(currentquota,1,0) OVER ( ORDER BY (select null)) from lag
LikeLiked by 1 person
Excellent solution : )
LikeLike
Hi pawan..in your SOLUTION 2 , i think we should use Inner Join instead of Left Outer join as Left Outer Join will display unwanted extra records. Please correct if i am wrong.
/************ SOLUTION 2 | Pawan Kumar Khowal ****************/
;WITH CTE AS
(
SELECT BusinessEntityID ,SalesYear ,CurrentQuota ,ROW_NUMBER()OVER (ORDERBY BusinessEntityID )AS ID
FROM lag
)
SELECT c.Id ,c.BusinessEntityID ,c.SalesYear , c.CurrentQuota,ISNULL(d.CurrentQuota,0) lagCurrentData
FROM CTE c LEFTOUTERJOIN CTE d ON c.ID =(d.ID+1)
LikeLike
with CTE_SALES as (
SELECT BusinessEntityID ,SalesYear ,CurrentQuota,
LAG (CurrentQuota,1) OVER (PARTITION BY BusinessEntityID ORDER BY BusinessEntityID, CurrentQuota) AS lagCurrentData
FROM lag)
SELECT BusinessEntityID ,SalesYear ,CurrentQuota,lagCurrentData from CTE_SALES
LikeLike
with CTE(BusinessEntityID,SalesYear,CurrentQuota)AS
(
SELECT ‘275’, ‘2005’,’367000′
UNION ALL
SELECT ‘275’, ‘2005’,’556000′
UNION ALL
SELECT ‘275’, ‘2006’,’502000′
UNION ALL
SELECT ‘275’, ‘2006’,’550000′
UNION ALL
SELECT ‘275’, ‘2006’,’1429000′
Union All
SELECT ‘275’, ‘2006’,’1324000′
)
select BusinessEntityID,SalesYear,CurrentQuota, (select max(b.CurrentQuota) from CTE b where b.CurrentQuota < a.CurrentQuota) as lagCurrentData
from CTE a
LikeLike