Link to home
Start Free TrialLog in
Avatar of acdagirl
acdagirl

asked on

need select statement

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...

Avatar of Nightman
Nightman
Flag of Australia image

Try this:

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

ASKER

WHERE OrderStatus='submitted' clause is too constricting - the status could be anything, not just submitted...
shouldn't the second t1 be a t2?
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...
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)
ASKER CERTIFIED SOLUTION
Avatar of Nightman
Nightman
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial