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
 Grouping

Author  Topic 

MangoSkin
Starting Member

22 Posts

Posted - 2010-07-26 : 18:52:11
I have 3 tables Order,Process,Items
Order 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 join
Items on Process.ProcessID=Items.ProcessID
group 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:


select
x.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 x
group by
x.OrderID
,x.Nof_Distinct_Process
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-07-27 : 01:09:32
quote:
Originally posted by MangoSkin

I have 3 tables Order,Process,Items
Order 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 join
Items on Process.ProcessID=Items.ProcessID
group 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
Go to Top of Page

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.
Go to Top of Page

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 @tbl
select 1,1 union all
select 2,3 union all
select 3,4 union all
select 1,5

select orderid,SUM(count(itemid))over() from @tbl
group by orderid




Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

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. :)
Go to Top of Page

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
Go to Top of Page

MangoSkin
Starting Member

22 Posts

Posted - 2010-07-27 : 10:46:16
Thanks I will check that.
Go to Top of Page
   

- Advertisement -