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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 ?!
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???
brandscoAuthor Commented:
can u also please tell me how to configure the table Net_Salary as a computed table please>?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

It's Pretty Simple. Just write an Update Trigger as show Below

IF ( UPDATE (Basic_salary) OR UPDATE (Util_Allow) OR UPDATE (Tran_allow) OR UPDATE (Deductions) )

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


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
see here:

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.
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)

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

Syed Mohammed Sirajuddin
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.