Solved

sql query question oracle

Posted on 2011-03-10
8
306 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to recover a database from a user managed backup

713 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