Which Transact-SQL query should you use?

A table named Profits stores the total profit made each year within a territory. The Profits table has
columns named Territory, Year, and Profit. You need to create a report that displays the profits
made by each territory for each year and its preceding year. Which Transact-SQL query should you
use?

A table named Profits stores the total profit made each year within a territory. The Profits table has
columns named Territory, Year, and Profit. You need to create a report that displays the profits
made by each territory for each year and its preceding year. Which Transact-SQL query should you
use?

A.
SELECT Territory, Year, Profit,
LAG(Profit, 1, 0) OVER(PARTITION BY Year ORDER BY Territory) AS NextProfit
FROM Profits

B.
SELECT Territory, Year, Profit,

LAG(Profit, 1, 0) OVER(PARTITION BY Territory ORDER BY Year) AS NextProfit
FROM Profits

C.
SELECT Territory, Year, Profit,
LEAD(Profit, 1, 0) OVER(PARTITION BY Territory ORDER BY Year) AS NextProfit
FROM Profits

D.
SELECT Territory, Year, Profit,
LEAD(Profit, 1, 0) OVER(PARTITION BY Year ORDER BY Territory) AS NextProfit
FROM Profits

Explanation:
http://msdn.microsoft.com/en-us/library/hh231256.aspx
http://msdn.microsoft.com/en-us/library/hh213125.aspx



Leave a Reply 8

Your email address will not be published. Required fields are marked *


Ghandi

Ghandi

Answer is B i think

Patty

Patty

B, but the alias of the column should be PrevProfit

Hai Dang

Hai Dang

B is wrong. The question request “each territory for each year and its preceding year”. LAG for Previous. LEAD for Preceding. C is correct

anna

anna

Not true. B is correct. Preceding MEANS previous