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
 General SQL Server Forums
 New to SQL Server Programming
 Aggregate on Subquery
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Metcalf
Yak Posting Veteran

USA
52 Posts

Posted - 04/25/2008 :  16:11:18  Show Profile  Reply with Quote
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)

Edited by - Metcalf on 04/25/2008 16:23:26

tkizer
Almighty SQL Goddess

USA
37274 Posts

Posted - 04/25/2008 :  16:13:30  Show Profile  Visit tkizer's Homepage  Reply with Quote

SELECT SUM(ExtPrice) AS ExtPrice, SUM(BoxCount) AS BoxCount
FROM
(
	YourQueryGoesHere
) t


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

tkizer
Almighty SQL Goddess

USA
37274 Posts

Posted - 04/25/2008 :  16:18:40  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

USA
52 Posts

Posted - 04/25/2008 :  16:28:15  Show Profile  Reply with Quote
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

USA
37274 Posts

Posted - 04/25/2008 :  16:29:31  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

USA
52 Posts

Posted - 04/25/2008 :  16:33:00  Show Profile  Reply with Quote
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!

Edited by - Metcalf on 04/25/2008 16:33:42
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30281 Posts

Posted - 04/25/2008 :  17:19:33  Show Profile  Visit SwePeso's Homepage  Reply with Quote
All tables must have a name or an alias.



E 12°55'05.25"
N 56°04'39.16"
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