By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,419 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,419 IT Pros & Developers. It's quick & easy.

How to add data from two coloumns

P: 4
Hi all, well am new to SQL.
There is a scenario, where i have to add values from two coloumns selected on the basis on particular ID and then add the total or average values of the resulted column.
Am using SQL SERVER 2005 developer edition.
and how to calculate percentage of each row of a column based on Particular ID
AND YES I HAVE TO WRITE THE SELECT QUERY FOR THE ABOVE SCENARIO.
THANKS
Dec 29 '09 #1

✓ answered by mwasif

Moving to SQL Server forum.

Share this Question
Share on Google+
2 Replies


mwasif
Expert 100+
P: 801
Moving to SQL Server forum.
Dec 29 '09 #2

nbiswas
100+
P: 149
Let me know if you are not looking for this

Expand|Select|Wrap|Line Numbers
  1. declare @t table(id int identity,col1 int,col2 int)
  2. insert into @t
  3.     select 10,20 union all select 11,21 union all
  4.     select 12,22 union all select 14, 24 union all select 15, 25
Query

Expand|Select|Wrap|Line Numbers
  1. select 
  2.          id
  3.         ,col1
  4.         ,col2
  5.         ,col1 + col2 AS [Total]
  6.         ,(col1+ col2) /2 AS [Avg]
  7.         ,col1 +  (col1 + col2) as [Total Added in Col1] 
  8.         ,col2 +  (col1 + col2) as [Total Added in Col2] 
  9.         ,col1 +  ((col1+ col2) /2) as [Avg Added in Col1] 
  10.         ,col2 +  ((col1+ col2) /2) as [Avg Added in Col2] 
  11.         ,CAST(col1 as float)/100 as [Col1 %]
  12.         ,CAST(col2 as float)/100 as [Col2 %]
  13.     from @t where id = 2
Output:

Expand|Select|Wrap|Line Numbers
  1. id    col1    col2    Total    Avg    Total Added in Col1    Total Added in Col2    Avg Added in Col1    Avg Added in Col2    Col1 %    Col2 %
  2. 2    11    21    32    16    43    53    27    37    0.11    0.21
  3.  
Note:~ First of all, I have a little doubt about the question you asked because you did not specify whether you want the entire column's total and Average and that you want to add with the Col1 & Col2 for a particular id or is it just horizontal addition for that id?

Please be specific while asking your question and if possible give a sample input with the expected output. It is easy to give an accurate solution.
Dec 30 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.