Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

sum of column in sql server

I have an employee table in which there is a column of salary name. Now I want the sum of the salary column in the last row but the total should be written in the row next to it as per the photo below where null is written.

select emp_name,SUM(emp_salary) as salary 
from   employe 
group by emp_name WITH ROLLUP 
order by emp_name desc

enter image description here

And the table format looks the same with the employee name


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use COALESCE:

coalesce(emp_name,'Total') as emp_name

In your query:

select coalesce(emp_name,'Total') as emp_name,
       SUM(emp_salary) as salary 
  from employe 
group by emp_name WITH ROLLUP 
order by emp_name desc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...