Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conditional outer join

Status
Not open for further replies.

invantix

Programmer
Joined
Mar 31, 2005
Messages
2
Location
US

Orders
OrderNumber
Description
...

Invoice
OrderNumber
Status
...

I want all orders for which there is no invoice record OR if there is an invoice record the status != 'C'

Thanks,

Chris
 
Hi Rudy,
your query doesn't work if there's an order with both status = C and status <> 'C'.

A slight modification fixes it:

select distinct
o.OrderNumber
, o.Description
from orders o
left outer
join Invoice i
on o.OrderNumber
= i.OrderNumber
where i.OrderNumber is null
or i.status <> 'c'


I couldn't find a single Exists query :-(

select *
from orders o
where not exists
(select * from invoice i
where o.OrderNumber = i.OrderNumber
)
or
exists
(select * from invoice i
where o.OrderNumber = i.OrderNumber
and i.Status <> 'C'
)


Dieter
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top