Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

sql query question oracle

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
Brant Snow
Asked:
Brant Snow
  • 4
  • 3
1 Solution
 
Alex MatzingerCommented:
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
 
Alex MatzingerCommented:
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
 
jimyXCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
jimyXCommented:
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
 
Brant SnowAuthor Commented:
jimmy neither of those are working, i am getting back duplicate order_number records
0
 
Alex MatzingerCommented:
Did you try mine thawts.  They should return what you are looking for
0
 
jimyXCommented:
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
 
Alex MatzingerCommented:
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now