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 strongm 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
Mar 31, 2005
2
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.

Part and Inventory Search

Sponsor

Back
Top