Solved

need select statement

Posted on 2007-03-28
6
203 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Suggested Solutions

Title # Comments Views Activity
SQL HELP 2 94
Testing connection to sql 7 61
Restrict result set 1 39
My Query is not giving correct result. Please help 5 51
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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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