Sum Of Numbers

#Uber/Tesla

Find the sum of numbers whose index is less than 5 and the sum of numbers whose index is greater than 5. Output each result on a separate row.

table name: transportation_numbers


Input:


Explanation:

select *, 
case when [index] <5 then 'x' else 'y' end as new_col
from transportation_numbers


Final Solution:

with cte as 
(
select *,
case when [index] <5 then 'x' else 'y' end as new_col
from transportation_numbers
)
select sum(number) as summm from cte group by new_col;

Final Output:


SQL Script:

USE [StrataScratch]
GO
CREATE TABLE [dbo].[transportation_numbers](
[index] [int] NULL,
[number] [int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[transportation_numbers] ([index], [number]) VALUES (1, 5)
GO
INSERT [dbo].[transportation_numbers] ([index], [number]) VALUES (2, 3)
GO
INSERT [dbo].[transportation_numbers] ([index], [number]) VALUES (3, 7)
GO
INSERT [dbo].[transportation_numbers] ([index], [number]) VALUES (4, 1)
GO
INSERT [dbo].[transportation_numbers] ([index], [number]) VALUES (5, 0)
GO
INSERT [dbo].[transportation_numbers] ([index], [number]) VALUES (6, 8)
GO
INSERT [dbo].[transportation_numbers] ([index], [number]) VALUES (7, 2)
GO
INSERT [dbo].[transportation_numbers] ([index], [number]) VALUES (8, 4)
GO
INSERT [dbo].[transportation_numbers] ([index], [number]) VALUES (9, 2)
GO


Comments (0)