Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Selecting rows

Posted on 2008-06-10
4
Medium Priority
?
243 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…

647 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