Calculate the sum at Row and column level.
Given Table:
Sal1
|
Sal2
|
10
|
15
|
20
|
30
|
15
|
30
|
30
|
40
|
Expected Output:
Sal1
|
Sal2
|
Sal3
|
10
|
15
|
25
|
20
|
30
|
50
|
15
|
30
|
45
|
30
|
40
|
70
|
75
|
115
|
190
|
Table Creation:
CREATE TABLE SalaryDetails
(
Sal1 INT,
Sal2 INT
)
INSERT INTO SalaryDetails VALUES(10,15)
INSERT INTO SalaryDetails VALUES(20,30)
INSERT INTO SalaryDetails VALUES(15,30)
INSERT INTO SalaryDetails VALUES(30,40)
Final Query:
WITH CTE
AS
(
SELECT Sal1, Sal2,Sal1+Sal2 as Sal3 FROM
SalaryDetails
)
SELECT Sal1, Sal2, Sal3 FROM CTE
UNION ALL
SELECT SUM(Sal1),SUM(Sal2),SUM(Sal3) FROM CTE
No comments:
Post a Comment