Solved

sql query question oracle

Posted on 2011-03-10
8
303 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle RAC 12c 8 56
Save conflict messages on existing documents 15 51
How to connect SQL Server from my Oracle database? 11 72
Oracle sql query 7 43
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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 explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

910 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

22 Experts available now in Live!

Get 1:1 Help Now