Groups | Blog | Home
all groups > sql server programming > october 2004 >

sql server programming : Summing two Sums


Adam Machanic
10/25/2004 5:11:36 PM
SELECT

[donation].[Month] AS Month,
[Donation].[Group] as group1,
SUM([donation].Amount) AS Qty,
SUM([donation_1].Amount) AS donation_amount,
[donation_1].Type,
SUM([donation_1].Amount) / SUM([donation].Amount) AS donation_average

FROM ...


--
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--


[quoted text, click to view]

Bogogboy
10/25/2004 9:54:12 PM
Hi Hope someone can point me in the right direction

I have a query that produces 2 sums
QTY and Donation_amount
I am trying to work out the average donation
therefore I would like to sum (donation_amount/QTY)
How Can I do this in the query
when I try adding this in the error message is invalid column name for
these.

I take it this is due to the query not producing the columns yet.

any help appreciated
here is the query that produces the 2 sums
Thanks in advance

Bogo.


SELECT

[donation].[Month] AS Month,
[Donation].[Group] as group1,
SUM([donation].Amount) AS Qty,
SUM([donation_1].Amount) AS donation_amount,
[donation_1].Type

FROM

[donation] INNER JOIN
[donation] [donation_1]
ON [donation].[Month] = [donation_1].[Month]
AND [donation].[branch] = [donation_1].[branch] AND
[donation].Sort = [donation_1].Sort AND [donation].[Group] =
[donation_1].[Group]

WHERE

([donation_1].Type <> N'numbers')
AND
([donation].Type = N'numbers')
AND
([donation].[Month] = CONVERT(DATETIME, '2004-07-01 00:00:00', 102))

GROUP BY

[donation].[Month],
[donation].[Group],
[donation_1].[Group],
[donation_1].Type,

Bogogboy
10/25/2004 10:21:03 PM
Hi Adam,

Thank you very much for your help

Regards,
Bogo.

[quoted text, click to view]

AddThis Social Bookmark Button