Solved

Selecting rows

Posted on 2008-06-10
4
219 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
ID: 21756674
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
ID: 21757488
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
ID: 21757556
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
ID: 31466014
Did just what I wanted!

Thanks
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

820 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