Solved

Selecting rows

Posted on 2008-06-10
4
231 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 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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…

717 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