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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 SUM and COUNT with different tables

Author  Topic 

timgaunt
Posting Yak Master

115 Posts

Posted - 2007-09-17 : 09:04:32
I'm sure I'm just not thinking today, but I have the following three tables (simplified):

Order
----------
OrderId
OrderName

Recipient
----------
RecipientId
OrderId
RecipientName

OrderItem
----------
RecipientId
Price

What I would like to do is produce a table including:
OrderId
OrderName
TotalRecipient (COUNT of Recipient.RecipientId)
OrderTotal (SUM of OrderItem.Price)

But the only way I can think of doing this without having a silly number of recipients is with multiple selects (or a view) within the same statement, is there a better way of doing this?

TIA

Tim

----------------------------
I've finally started blogging (all be it badly!)

Check it out:
http://blogs.thesitedoctor.co.uk/tim/

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-17 : 09:51:11
[code]SELECT o.OrderId,
o.OrderName,
COUNT(r.OrderId) AS TotalRecipient,
SUM(ISNULL(oi.Price, 0)) AS OrderTotal
FROM Order AS o
LEFT JOIN Recipient AS r ON r.OrderId = o.OrderId
LEFT JOIN OrderItem AS oi ON oi.RecipientId = r.RecipientId[/code]


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

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2007-09-17 : 09:51:33
This is a start, but you will still be Cartesian since there is no way to uniquely link a recipientid to an orderid. You would be okay without grouping on the orderid, or by adding orderid to recipient table

SELECT o.orderid
,o.ordername
,a.TotalRecipient
,b.OrderTotal
FROM
[order] o
INNER JOIN
(select orderid,recipentid,'TotalRecipent' = count(recipientid) from recipient group by orderid,recipentid) a
ON
o.orderid = a.orderid
INNER JOIN
(select recipientID,'OrderTotal' = sum(Price) from OrderItem group by recipientID) b
ON
a.recipientid = b.recipientid


Jim
Go to Top of Page

timgaunt
Posting Yak Master

115 Posts

Posted - 2007-09-17 : 10:12:48
Peso thanks, I should have included my query because that's what I was doing, the issue being if there are multiple items per recipient (which there will be) it increases the count

jimf -thanks, I've also done something along those lines however it seemed a little OTT having 3 selects for 1...

quote:
there is no way to uniquely link a recipientid to an orderid


I take it you mean OrderItem not Recipient as OrderId is in the Recipient table. I decided not to include it in the OrderItem table is it felt like I was duplicating data...

Tim

----------------------------
I've finally started blogging (all be it badly!)

Check it out:
http://blogs.thesitedoctor.co.uk/tim/
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-09-17 : 10:40:58
Tim -- try reading the two part article here:

http://www.sqlteam.com/article/how-to-use-group-by-in-sql-server

It covers exactly what you need; it starts off with the basics, but it should show you exactly how to handle what you need.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

timgaunt
Posting Yak Master

115 Posts

Posted - 2007-09-17 : 11:34:57
Exactly what I was after ta, COUNT(DISTINCT...) did the trick.

Tim

----------------------------
I've finally started blogging (all be it badly!)

Check it out:
http://blogs.thesitedoctor.co.uk/tim/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-17 : 11:36:09
The article is all about NOT using "COUNT DISTINCT"...



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

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-17 : 11:50:32
Also, how are you going to distinguish between several same RecepientID between orders?



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

itzmohikumar
Starting Member

3 Posts

Posted - 2007-09-20 : 04:29:25
Is it the same case when we are taking SUM from different child tables with one-many relationship?

Will this not effect the performance of the query exceution if we have approximately 1 million record in each table?

Is there any other alternate solution for this?

Can anybody please, help me on sorting out this problem.

Thanks in advance
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-09-20 : 07:18:43
Read both parts of the article I linked to. Also, this may be helpful to you: http://weblogs.sqlteam.com/jeffs/archive/2006/06/19/10270.aspx

It depends on what specifically you are trying to do. Start a new thread with your specific question if you need assistance.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -