Summation expressions in select statement

neoklis

Registered User.
Local time
Tomorrow, 01:12
Joined
Mar 12, 2007
Messages
80
Hi guys,

Can someone quide me how can i sum two expression collumns into a statement?

a=1
b=2

SELECT 1 AS a, 2 AS b

ok so far. The problem is when i add one more collumn to store the sum. I can't figure whow am i going to phrase into the select statement. The below returns error.

Total=a+b

SELECT
1 AS a, 2 AS b, a+b as total

Thank you
 
You cannot do this... atleast that I know...
Two options to 'work around' the problem

1) Re-enter expressions
Select 1 as a, 2 as b, 1 + 2 as total

2) Subquery
Select a,b,a+b as total
from
( SELECT 1 AS a, 2 AS b ) as x
 
Thank's a lot for your help!! The second way is what i need..! So I have to play with subquery. I'll test it and see what am i going to catch..

Thanks!
 
In Ms Access I used to have the result of a function in an expression column. The result of the funcion I could use it to a next column.. into a next column and so far… very simply by just writing the name of the column… What I see in action with subquery is that it is not so simply as I used to know.. or I may abstract something..

When I run the below T-Sql runs great..

SELECT Material_Group, MRP_Controller, Description,dbo.Perf (Material_Group,MRP_Controller,Description) AS Vtakin
FROM Perf_PerformanceView

The problem comes when I want to use the value of Vtakin column to next columns. What if I want to add a Vtakin1,Vtakin2 columns that call functions that use the value of vtakin column..?

Example:

Vtakin1=Dbo.Perf1(Vtakin)
Vtakin2= Dbo.Perf2(Vtakin)


Thank you
 
You have to use either the original input or a subselect (like I showed you earlier)
Code:
select 
  Dbo.Perf1(Vtakin) as Vtakin1
, Dbo.Perf2(Vtakin) as Vtakin2
from (
   SELECT Material_Group, MRP_Controller, Description
   ,dbo.Perf(Material_Group,MRP_Controller,Description) AS Vtakin
   FROM Perf_PerformanceView ) as x

or
Code:
SELECT Material_Group, MRP_Controller, Description
,            dbo.Perf (Material_Group,MRP_Controller,Description)  AS Vtakin
, dbo.Perf1 (dbo.Perf (Material_Group,MRP_Controller,Description)) as Vtakin1
, dbo.Perf2 (dbo.Perf (Material_Group,MRP_Controller,Description)) as Vtakin2
FROM Perf_PerformanceView

Or something like that anyway...

** Disclaimer **
As far as I know...

Regards
 

Users who are viewing this thread

Back
Top Bottom