Solved

need select statement

Posted on 2007-03-28
6
212 Views
Last Modified: 2010-03-19
i have a table in sql 2005 with the following fields:

tblOrders: Customer, OrderStatus, Date

I need to find all orders that have been resubmitted once rejected, by Customer. For example:

Customer     OrderStatus     Date
abc group    rejected            12/12/2006
fgh inc         pending             12/13/2006
abc group   submitted           1/7/2007
fgh inc        submitted           1/7/2006
fgh inc        rejected             1/9/2006

the result here should be only:
abc group because they are the only ones that were rejected at some date in the past and then resubmitted.
fgh in would not be in this list because following their rejection they did not resubmit anything...

0
Comment
Question by:acdagirl
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 18809563
Try this:

SELECT * FROM tblOrders t1 WHERE OrderStatus='submitted' and exists (SELECT * FROM tblOrders t1 WHERE OrderStatus='rejected' AND t1.Customer=t2.Customer)
0
 

Author Comment

by:acdagirl
ID: 18809587
WHERE OrderStatus='submitted' clause is too constricting - the status could be anything, not just submitted...
0
 

Author Comment

by:acdagirl
ID: 18809613
shouldn't the second t1 be a t2?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:acdagirl
ID: 18809630
but I also need to account for the timeline... your query doesn't exclude orders that were resubmitted *after* they were rejected only. It yields all customers that have at any point been rejected and/or accepted...
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18809632
yes. Sorry - long day :)

And if the OrderStatus='submitted' is too restrictive, simply remove it.

SELECT * FROM tblOrders t1 WHERE EXISTS (SELECT * FROM tblOrders t2 WHERE OrderStatus='rejected' AND t1.Customer=t2.Customer)
0
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 18809681
How about this?

SELECT * FROM tblOrders t1 WHERE OrderStatus<>'rejected' AND EXISTS (SELECT * FROM tblOrders t2 WHERE OrderStatus='rejected' AND t1.Customer=t2.Customer AND t2.Date<t1.Date)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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