How to add data from two coloumns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anuragvatsa
    New Member
    • Dec 2009
    • 4

    How to add data from two coloumns

    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
    Last edited by anuragvatsa; Dec 29 '09, 08:46 AM. Reason: question updated
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Moving to SQL Server forum.

    Comment

    • nbiswas
      New Member
      • May 2009
      • 149

      #3
      Solution to How to add data from two coloumns

      Let me know if you are not looking for this

      Code:
      declare @t table(id int identity,col1 int,col2 int)
      insert into @t
      	select 10,20 union all select 11,21 union all
      	select 12,22 union all select 14, 24 union all select 15, 25
      Query

      Code:
      select 
      		 id
      		,col1
      		,col2
      		,col1 + col2 AS [Total]
      		,(col1+ col2) /2 AS [Avg]
      		,col1 +  (col1 + col2) as [Total Added in Col1] 
      		,col2 +  (col1 + col2) as [Total Added in Col2] 
      		,col1 +  ((col1+ col2) /2) as [Avg Added in Col1] 
      		,col2 +  ((col1+ col2) /2) as [Avg Added in Col2] 
      		,CAST(col1 as float)/100 as [Col1 %]
      		,CAST(col2 as float)/100 as [Col2 %]
      	from @t where id = 2
      Output:

      Code:
      id	col1	col2	Total	Avg	Total Added in Col1	Total Added in Col2	Avg Added in Col1	Avg Added in Col2	Col1 %	Col2 %
      2	11	21	32	16	43	53	27	37	0.11	0.21
      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.

      Comment

      Working...