?
Solved

Selecting rows

Posted on 2008-06-10
4
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

762 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