Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k 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 OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...