SQL Server Important and common interview queries

Question 1:Write a query which will produce the below output ?











Solution - We can achieve the above situation by the following ways -

Query 1
Select 
  T0.ID 
 ,T0.Num
,(Select SUM(Num) from #Temp T1 where T1.Id<=T0.ID) as Result
From #temp T0
Query 2
Select
T0.ID
,T0.Num
,SUM(T1.Num) as Result
from #Temp T0 
Inner Join #Temp T1 on T1.Id<=T0.Id
Group by T0.ID ,T0.Num

Post a Comment

0 Comments