Solved

sql query question oracle

Posted on 2011-03-10
8
307 Views
Last Modified: 2012-06-27
table_structure

so here is my table structure, see image, 2 tables called order_header and order_lines
order_lines foreign key is order_number and order_header's primary key is order_number, it is a one to many relationship between order_header and order_lines

I need to get the result table based off of the following information

order_interval = 3 and item_code of 'burger' is not present within any order_number record.

So you can see by the result, only order_number's of 345 and 342 have a order_interval of 3 but order_number 342 does have a line item with 'burger' in it so it should not be returned, order_number 345 does not have a line item with 'burger' in it so it is returned

This is for a oracle database, what would be the sql to use
0
Comment
Question by:Brant Snow
[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
  • 4
  • 3
8 Comments
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35097902
Select Order_number, cust_id, order_interval
From order_header oh, order_lines ol
where oh.Order_number = ol.Order_number
   AND oh.Order_interval =3
   AND ol.item_code != 'burger'
0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35097932
Sorry, i just realized my mistake...try this

Select Order_number, cust_id, order_interval
From order_header oh, order_lines ol
where oh.Order_number = ol.Order_number
   AND oh.Order_interval =3
   AND 'burger' NOT IN (
                                        SELECT Item_code
                                        from order_lines temp
                                        where temp.Order_number = oh.order_number)
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35097944
select l.order_number, h.cust_id, h.order_interval from order_header h, order_lines l where h.order_interval = 3 and l.item_code <> 'burger' and l.order_number = h.order_number
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 24

Expert Comment

by:jimyX
ID: 35098376
After testing:
select h.order_number, h.cust_id, h.order_interval from order_header h where h.order_interval = 3 and h.order_number not in (select l.order_number from order_lines l where l.item_code = 'burger')
0
 
LVL 4

Author Comment

by:Brant Snow
ID: 35098442
jimmy neither of those are working, i am getting back duplicate order_number records
0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35098456
Did you try mine thawts.  They should return what you are looking for
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 35098502
I have duplicated your table structure with its data and tried this line and it gets the exact result you are showing up there:
select h.order_number, h.cust_id, h.order_interval from order_header h where h.order_interval = 3 and h.order_number not in (select l.order_number from order_lines l where l.item_code = 'burger')

Open in new window

0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35098524
Select oh.Order_number, oh.cust_id, oh.order_interval
From order_header oh, order_lines ol
where oh.Order_number = ol.Order_number
   AND oh.Order_interval =3
   AND 'burger' NOT IN (
SELECT Item_code
from order_lines temp
where temp.Order_number = oh.order_number)

Open in new window

0

Featured Post

Industry Leaders: 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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

752 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