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.
| Author |
Topic |
|
MangoSkin
Starting Member
22 Posts |
Posted - 2010-07-26 : 18:52:11
|
| I have 3 tables Order,Process,ItemsOrder has one to many relation with Process,Process has one to many relation with Items.I want to see the number of Items in an Order.Select OrderID,count(ProcessID), sum(count(ItmeID))from Order inner join Process on Order.OrderID=Process.OrderID inner joinItems on Process.ProcessID=Items.ProcessIDgroup by Order.OrderID.I am having issue with summing the Items part.Can someone help? |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-27 : 00:08:10
|
your issue is that you can't sum and count at the same time. either you use sum(ItemID) or count(ItemID) or do this in two steps with subquery, something like:selectx.OrderID,Nof_Distinct_Process,sum(NOf_Items) from( Select OrderID as orderID ,count(distinct ProcessID) as NOf_distinct_Process ,count(ItemID) as NOf_Items from Order inner join Process on Order.OrderID=Process.OrderID inner join Items on Process.ProcessID=Items.ProcessID group by Order.OrderID.) as xgroup by x.OrderID,x.Nof_Distinct_Process |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-07-27 : 01:09:32
|
quote: Originally posted by MangoSkin I have 3 tables Order,Process,ItemsOrder has one to many relation with Process,Process has one to many relation with Items.I want to see the number of Items in an Order.Select OrderID,count(ProcessID),sum(count(ItmeID))over()from Order inner join Process on Order.OrderID=Process.OrderID inner joinItems on Process.ProcessID=Items.ProcessIDgroup by Order.OrderID.I am having issue with summing the Items part.Can someone help?
Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-07-27 : 01:18:52
|
What should that be?sum(count(ItmeID))over() No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-07-27 : 01:21:52
|
quote: Originally posted by webfred What should that be?sum(count(ItmeID))over() No, you're never too old to Yak'n'Roll if you're too young to die.
The OP wanted the sum of all the counts for all the itemid if I am not mistaken.With SUM(count(itemid))over() it will return the the sum count of items & display it against each orderid.declare @tbl as table(orderid int,itemid int)insert into @tblselect 1,1 union allselect 2,3 union allselect 3,4 union allselect 1,5select orderid,SUM(count(itemid))over() from @tblgroup by orderid Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-27 : 01:44:35
|
| Idera,this query sure is valid and okey, but i hope the MangoSkin understands the value and results of the query. :) |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-07-27 : 02:35:52
|
quote: Originally posted by slimt_slimt Idera,this query sure is valid and okey, but i hope the MangoSkin understands the value and results of the query. :)
And also I hope he is using SQL 2005 & above for my query to work. Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
MangoSkin
Starting Member
22 Posts |
Posted - 2010-07-27 : 10:46:16
|
| Thanks I will check that. |
 |
|
|
|
|
|
|
|