Solved

need select statement

Posted on 2007-03-28
6
215 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
[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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS report parameters set after publishing to report manager 1 80
Sql query 107 100
SQL Server Configuration Manager WMI Error 11 51
Database Mail Profiles 1 44
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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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