?
Solved

complicated Select Statement Order-Order Details

Posted on 2007-04-04
7
Medium Priority
?
634 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 300 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Assisted Solution

by:sujith80
sujith80 earned 300 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 300 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 300 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 300 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to recover a database from a user managed backup
Suggested Courses

771 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