Solved

sql query question oracle

Posted on 2011-03-10
8
302 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
  • 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Read about achieving the basic levels of HRIS security in the workplace.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now