Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Aggregate on Subquery

Author  Topic 

Metcalf
Yak Posting Veteran

52 Posts

Posted - 2008-04-25 : 16:11:18
I want to return only the sum total of each of the following two columns generated by this query, but when I wrap them in SUM() I get an error stating that I can't use an aggregate function on an aggregate or subquery.

Is there another approach that I might take to sum these?

SELECT 		CASE soitem.fmultiple 
WHEN 1 then
(SELECT funetprice FROM sorels
WHERE (sorels.fsono = shmast.fcsono)
AND sorels.frelease
= SUBSTRING(shitem.fsokey,10,3)
AND sorels.fenumber
= shitem.fenumber) * shitem.fshipqty
ELSE
(SELECT top 1 funetprice FROM sorels
WHERE (sorels.fsono = shmast.fcsono)
AND sorels.finumber
= soitem.finumber) * shitem.fshipqty
END as ExtPrice,


CASE CAST((shitem.fshipqty) as int)
% nullif(CAST(inmast.fnusrqty1 as int),0)
WHEN 0 then
(CAST((shitem.fshipqty) as int) /
nullif(CAST(inmast.fnusrqty1 as int),0))
ELSE
(CAST((shitem.fshipqty) as int) /
nullif(CAST(inmast.fnusrqty1 as int),0)) + 1
END as BoxCount

FROM shmast INNER JOIN shitem ON shmast.fshipno = shitem.fshipno
INNER JOIN soitem ON (soitem.fsono = shmast.fcsono)
AND (Convert(Int,soitem.finumber) = Convert(Int,SUBSTRING(shitem.fsokey,8,10)) / 1000)
LEFT JOIN somast ON (shmast.fcsono = somast.fsono)
LEFT JOIN inmast ON (soitem.fpartno = inmast.fpartno)
WHERE (shmast.fbl_lading='00000000000000003784')
AND (shitem.fshipqty > 0)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-25 : 16:13:30
[code]
SELECT SUM(ExtPrice) AS ExtPrice, SUM(BoxCount) AS BoxCount
FROM
(
YourQueryGoesHere
) t
[/code]

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-25 : 16:18:40
You've edited your query in your post. So now you've got to remove those SUMs you just added in order for my solution to work.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Metcalf
Yak Posting Veteran

52 Posts

Posted - 2008-04-25 : 16:28:15
quote:
Originally posted by tkizer

You've edited your query in your post. So now you've got to remove those SUMs you just added in order for my solution to work.



Oops; I was trying to edit it so that the code block wouldn't be 15000 chars wide, and pasted the broken version in by mistake.

As to your suggestion, I'm attempting to try it but I keep getting a syntax error; it thinks I have a stray ')' in there somewhere. I'll postback when I get it to work, now that you've posted it I feel silly for not having thought of that before bringing it up in public.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-25 : 16:29:31
Make sure you alias the derived table as I have with t. Some people think that it's a typo when I post a solution like that, but it is required. You just don't have to name it t.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Metcalf
Yak Posting Veteran

52 Posts

Posted - 2008-04-25 : 16:33:00
That worked, thank you. If you don't mind my asking, what is the significance of the 't' past the closing parenthesis; I'd originally taken it to be a typo, but its exclusion is what was getting me the syntax error near ')'.

Edit: Ah, you are all over this one. Thank you very much for your help!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-25 : 17:19:33
All tables must have a name or an alias.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -