Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread select question
Tue, Jul 24 2007 8:56 AMPermanent Link

"Harry de Boer"
LS

Tables: Customers, Orders, Order_Detail

How do I create a select statement that shows id_customer, id_order but only
when there is more then 1 orderdetail record?

Regards, Harry

Tue, Jul 24 2007 10:26 AMPermanent Link

"Ole Willy Tuv"
Harry,

<< Tables: Customers, Orders, Order_Detail

How do I create a select statement that shows id_customer, id_order but only
when there is more then 1 orderdetail record? >>

A couple of alternatives:

1)

select
 c.CustomerID,
 o.OrderID
from
 Customers c
 join Orders o on o.CustomerID = c.CustomerID
 join Order_Detail od on od.OrderID = o.OrderID
group by c.CustomerID, o.OrderID
having count(od.OrderID) > 1

2)

select
 c.CustomerID,
 o.OrderID
from
 Customers c
 join Orders o on o.CustomerID = c.CustomerID
where o.OrderID in
(
 select OrderID
 from Order_Detail
 group by OrderID
 having count(OrderID) > 1
)

Ole Willy Tuv

Tue, Jul 24 2007 11:31 AMPermanent Link

"Harry de Boer"
Ole,

Thanks. I'll go for alternative 1 (seems to be a bit faster).

Regards, Harry


"Ole Willy Tuv" <owtuv@online.no> schreef in bericht
news:81B529EC-4ED4-44FB-81EC-E2F7A07AC69F@news.elevatesoft.com...
> Harry,
>
> << Tables: Customers, Orders, Order_Detail
>
> How do I create a select statement that shows id_customer, id_order but
only
> when there is more then 1 orderdetail record? >>
>
> A couple of alternatives:
>
> 1)
>
> select
>   c.CustomerID,
>   o.OrderID
> from
>   Customers c
>   join Orders o on o.CustomerID = c.CustomerID
>   join Order_Detail od on od.OrderID = o.OrderID
> group by c.CustomerID, o.OrderID
> having count(od.OrderID) > 1
>
> 2)
>
> select
>   c.CustomerID,
>   o.OrderID
> from
>   Customers c
>   join Orders o on o.CustomerID = c.CustomerID
> where o.OrderID in
> (
>   select OrderID
>   from Order_Detail
>   group by OrderID
>   having count(OrderID) > 1
> )
>
> Ole Willy Tuv
>
>

Image