Sum of Rows in Different Columns of a single table in SQL Server

Hi All

I have a test environment SQL Server 2008 R2 Express Edition. I have attached a snapshot of the table below. Here i have some columns which are having salary details. I have a seperate column where there is a total salary column.

I want the green band added and red band subtracted and the blue band to be the total.
I am a newbie to sql.. please help
 SQL Rows
LVL 1
brandscoAsked:
Who is Participating?
 
santhimurthydCommented:
It's Pretty Simple. Just write an Update Trigger as show Below

 
CREATE TRIGGER EMP_DATA ON UPDATESALARY
     AFTER UPDATE
 AS
     SET NOCOUNT ON ;  
   
IF ( UPDATE (Basic_salary) OR UPDATE (Util_Allow) OR UPDATE (Tran_allow) OR UPDATE (Deductions) )
BEGIN

  update EMP_DATA
  set net_salary = ((Basic_salary + Util_Allow + Tran_allow) - Deductions)

end

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
simple "math":
update yourtable
  set net_salary = basic_salary + util_allow + tran_allow - deductions

Open in new window


however, such a column should be a computed column ?!
0
 
brandscoAuthor Commented:
i have the column called Net_Salary.. can i have this column to set it in such a way that when i update the other 4 columns which is in red and green, it must computer automatically.. by using the command that you have given.. only once it will compute ami right>? i want to create 70 more records and each time i enter the values in salary components and deductions it must come in the net_salary column. Is it possible???
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
brandscoAuthor Commented:
can u also please tell me how to configure the table Net_Salary as a computed table please>?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
see here:
http://msdn.microsoft.com/en-us/library/ms191250.aspx

in short, for your status:
drop the existing column, and add it again, but not with a data type, but with the expression indicated.

note however that I consider this column a "bad" column: it's against normalization.
you might have another table which, per month, indicates the total salary given for the employee (as the salary might change every month), with even all of the details there, but in the employee table, this net_salary should not exist.
0
 
AStaCommented:
As i see all columns may contain NULL. May be somthing like

update dbo.EMP_DATA
  set net_salary = ISNULL(basic_salary, 0) + ISNULL(util_allow, 0) + ISNULL(tran_allow, 0) - ISNULL(deductions, 0)

0
 
brandscoAuthor Commented:
Ok. I understood. Sure i will work on this knowledge that you have provided me.
I want to become an SQL Expert.

I will get back to you all in a day's time
Thanks

Syed Mohammed Sirajuddin
0
 
santhimurthydCommented:
your are welcome as to contrinbute on SQL area. It's pretty simple spend soem tiem and keep watching the SQL querries and try to answer which you feel comfort. Day by Day you will become what you wish.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.