Solved

Selecting rows

Posted on 2008-06-10
4
188 Views
Last Modified: 2010-04-21
I have a Sales and an Inventory table that I want to select only Sales rows that match multiple inventory items.

i.e. the Sales table has sales order number 1234 and the Inventory table has multiple rows that all have sales order number 1234 but one row has a status of "Sales_Hold" another has a row with a status of "CC_PMT" and another row with a status of  "COD". I have a second Sales order number 5432 and the Inventory table has multiple rows that all have sales order 5432 but one row has a status of "COD" and another row with a status of "RELEASED".

I want to select a sales order if a matching inventory row has a status of "COD" but not if there is also a matching inventory row with a status of "Sales_Hold".

In this example sales order 5432 would be true but sales order 1234 would be false.

Hope this is clear.

Any help is appreciated.

Thanks
0
Comment
Question by:jdr0606
4 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
Comment Utility
select * from sales s
join inventory i on s.ordernumber = i.ordernumber
where status = 'cod'
and not exists(select 1 from inventory ii where i.ordernumber = ii.ordernumber and status = 'sales_hold')
0
 
LVL 1

Expert Comment

by:hasander
Comment Utility
In SQL Server 2005 you can use EXCEPT to exclude orders that is "sales_hold"

select * from sales s
join inventory i on s.ordernumber = i.ordernumber
where status = 'cod'
EXCEPT
select * from sales s
join inventory i on s.ordernumber = i.ordernumber
where status = 'sales_hold'
0
 
LVL 1

Expert Comment

by:Peter861
Comment Utility
Try this one, it should work

select s.* from Sales s inner join Inventory i on s.OrderNumber = i.OrderNumber where i.Status = 'COD' and s.OrderNumber not in (select distinct ii.OrderNumber from Inventory ii where ii.Status = 'Sales_Hold')
0
 

Author Closing Comment

by:jdr0606
Comment Utility
Did just what I wanted!

Thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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

7 Experts available now in Live!

Get 1:1 Help Now