| Author |
Topic |
|
shemayb
Posting Yak Master
159 Posts |
Posted - 2007-11-29 : 13:23:28
|
Can i combine two aggregate functions in one select statement?Like sum(count (field 1) * field 2) as tcost.Can i do something like that in my query?thanks! Funnyfrog |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-11-29 : 13:27:42
|
| Yes you can have more than one aggregate function in one query.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
shemayb
Posting Yak Master
159 Posts |
Posted - 2007-11-29 : 13:30:55
|
| i tried it but it didn't work..Funnyfrog |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-11-29 : 13:35:15
|
| Saying "it didn't work" doesn't help us help you. Please provide the error and the full query that you tried.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
shemayb
Posting Yak Master
159 Posts |
Posted - 2007-11-29 : 13:37:51
|
| Select sum(count(cp.pid) * cp.mr) as ptcost from caregiverpayroll cpinner join visittype vt on vt.tid = cp.tid inner join visits v on v.apid = cp.apidWhere actual_visit_date between '01/01/2000' and '12/31/2007' AND active=1 AND visited=1 and vt.ctid = '421'Group by cp.pid,cp.mrthat is my query..Funnyfrog |
 |
|
|
shemayb
Posting Yak Master
159 Posts |
Posted - 2007-11-29 : 13:38:42
|
| this is the error of the query that i previously poaste:Cannot perform an aggregate function on an expression containing an aggregate or a subquery.Funnyfrog |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-11-29 : 13:45:12
|
| [code]Select sum(ptcost) as sumptcostfrom( select (count(cp.pid) * cp.mr) as ptcost from caregiverpayroll cp inner join visittype vt on vt.tid = cp.tid inner join visits v on v.apid = cp.apid Where actual_visit_date between '01/01/2000' and '12/31/2007' AND active=1 AND visited=1 and vt.ctid = '421' Group by cp.pid,cp.mr) t[/code]Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
shemayb
Posting Yak Master
159 Posts |
Posted - 2007-11-29 : 13:49:32
|
thanks! i will try it.. Funnyfrog |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
shemayb
Posting Yak Master
159 Posts |
Posted - 2007-11-29 : 14:20:00
|
| Select sum(ptcost) as sumptcost,sum(ptvisits)from( select count(cp.cpid)as ptvisits,(count(cp.pid) * cp.mr) as ptcost from caregiverpayroll cp inner join visittype vt on vt.tid = cp.tid inner join visits v on v.apid = cp.apid Where actual_visit_date between '01/01/2000' and '12/31/2007' AND active=1 AND visited=1 and vt.ctid = '421' Group by cp.pid,cp.mr) tif i have the query above,can i add another select statement above all of these to get the average of sum(ptcost) as sumptcost and sum(ptvisits)?Funnyfrog |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-11-29 : 14:35:12
|
| You can get average of sum(ptcost), but you can't get sum(ptvisits) since it isn't being calculated in the derived table. If you'd provide a data example of what the inner query produces and then show us exactly what you want outputted, we can help provide a working solution. Also, make sure you read the article Jeff posted rather than just asking us questions.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
|