SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 SUM on AS column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dbonneau
Yak Posting Veteran

50 Posts

Posted - 07/05/2013 :  11:37:58  Show Profile  Reply with Quote
Hi, I want to group by MVP, but I get error because I need to sum MVP first. So put sum(MVP), but doesn't work.. Could anyone tell me how to do that ? Thanks.


select 
LS,
(CAST(IV AS float) +
 CAST (EH as float) +
 CAST(OE as float)) as MVP
from Table1
Group by LS

James K
Flowing Fount of Yak Knowledge

3715 Posts

Posted - 07/05/2013 :  11:44:41  Show Profile  Reply with Quote
quote:
Originally posted by dbonneau

Hi, I want to group by MVP, but I get error because I need to sum MVP first. So put sum(MVP), but doesn't work.. Could anyone tell me how to do that ? Thanks.


select 
LS,
(CAST(IV AS float) +
 CAST (EH as float) +
 CAST(OE as float)) as MVP
from Table1
Group by LS



The way the query is written, it will have two columns, LS and MVP, with one row for each distinct value of LS.

When you say you want to group by MVP, what should the output look like? For example, if your current output is like shown below, what is the new output you are expecting?
LS  MVP
1   11.7
2   11.7
3   15.2
4   19.1
Go to Top of Page

dbonneau
Yak Posting Veteran

50 Posts

Posted - 07/05/2013 :  11:52:19  Show Profile  Reply with Quote
quote:
Originally posted by James K

quote:
Originally posted by dbonneau

Hi, I want to group by MVP, but I get error because I need to sum MVP first. So put sum(MVP), but doesn't work.. Could anyone tell me how to do that ? Thanks.


select 
LS,
(CAST(IV AS float) +
 CAST (EH as float) +
 CAST(OE as float)) as MVP
from Table1
Group by LS



The way the query is written, it will have two columns, LS and MVP, with one row for each distinct value of LS.

When you say you want to group by MVP, what should the output look like? For example, if your current output is like shown below, what is the new output you are expecting?
LS  MVP
1   11.7
2   11.7
3   15.2
4   19.1




Hi, I am sorry.. What I meant was group by LS. So, I need to sum on MVP to get an output something like you described.. thank you

EX.

LS MVP
James 23
John 11
David 50

Thanks.
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/05/2013 :  12:22:38  Show Profile  Reply with Quote
Where are you getting the names
James, John and David in you output?

LS MVP
James 23
John 11
David 50

Go to Top of Page

dbonneau
Yak Posting Veteran

50 Posts

Posted - 07/05/2013 :  12:23:42  Show Profile  Reply with Quote
Yes. LS is varchar. Thanks
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/05/2013 :  12:28:05  Show Profile  Reply with Quote
Is this what you want:


SELECT LS, SUM(MVP) FROM 
(select 
LS,
(CAST(IV AS float) +
 CAST (EH as float) +
 CAST(OE as float)) as MVP
from Table1)T
Group by LS



Edited by - MuMu88 on 07/05/2013 12:28:43
Go to Top of Page

dbonneau
Yak Posting Veteran

50 Posts

Posted - 07/05/2013 :  15:18:48  Show Profile  Reply with Quote
quote:
Originally posted by MuMu88

Is this what you want:


SELECT LS, SUM(MVP) FROM 
(select 
LS,
(CAST(IV AS float) +
 CAST (EH as float) +
 CAST(OE as float)) as MVP
from Table1)T
Group by LS







Thank you ! It works good !
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000