Solved

complicated Select Statement Order-Order Details

Posted on 2007-04-04
7
630 Views
Last Modified: 2013-12-19
What is the query to retrive? All orders that, do not display orders where all the order_Details contain a code = ERD.

Order Table
OrderID OrderName
01            OfficeOrderA
02            OfficeOrderB
03            OfficeOrderC

Order_Detail Table
OrDetailID OrderID ItemName Code1            
01               01         pencil      ERD
02               01         ballpen       ERD
03               01         stappler      CDG
04               02         chair      ERD
05               02         table      ERD


Do dot display orders where all the order_Details
contain a code = ERD.

Select * from Orders where (?)

Thanks,
BG
0
Comment
Question by:blueglacier
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 18854137
select o.* from Orders o where o.OrderID not in (select orderID from Order_Detail od where od.Code1 = 'ERD')
0
 
LVL 1

Author Comment

by:blueglacier
ID: 18854479
Ray

I want to Discard those orders that have all their Order Detail rows as ERD.

On the example tables above, only Order 1 will be returned. Becuase Order 2 has all the Order Details with code = ERD.

your solution will elimitate order 1 and 2.

BG
0
 
LVL 35

Accepted Solution

by:
Raynard7 earned 100 total points
ID: 18854600
sorry - so you want ALL the order details as erd

select o.* from Orders o where o.OrderID not in
(
select
    x.orderID
from
(
select od1.orderID, count(*) cx, sum(case when od1.code1 = 'ERD' then 1 else 0 end)  sx from  Order_Detail od1 group by od1.orderID
) x
where x.cx = x.sx
)

may work
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Assisted Solution

by:sujith80
sujith80 earned 100 total points
ID: 18855554
select a.*
from orders a, (select distinct orderId from order_details where code1 <> 'ERD') b
where a.orderId = b.orderId
0
 
LVL 3

Assisted Solution

by:kimarti3
kimarti3 earned 100 total points
ID: 18857675
select * from orders where order_id in (select order_id from order_details where code1 != 'ERD')
0
 
LVL 7

Assisted Solution

by:RainMan82
RainMan82 earned 100 total points
ID: 18857735
select *
  from order a,
       order_detail b
 where a.orderid = b.orderid
   and a.orderid not in (select distinct x.orderid from order_detail x where x.code1 = 'ERD')
0
 

Assisted Solution

by:desiboy1974
desiboy1974 earned 100 total points
ID: 18863480
select *
  from order a
  where not exists(select 1 from order_detail b
where b.order_id=a.order_id
and b.code='ERD')
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to recover a database from a user managed backup

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question