ORACLE 11G RETURNING A RESULTSET IF BOTH CONDITIONS ARE TRUE IN DIFFERENT ROWS

Table
Empid          City                    Type                  Status
1245              Dallas               OrderTo          CheckedOut
1245              New York         CheckedIn    InTransit
1111               Dallas               CheckedIn     Temporary
 1439               Dallas               OrderTo        CheckedOut
  1439             New York         CheckedIn    InTransit
  1880             DeRidder          OrderTo       CheckedOut
   1883            Coushata          CheckedIn   InTransit

When  given two Cities, for example,  Dallas and New York
From these input I need to bring back the Empid that meets the following requirement:

Both must be true:
Their type and status must  OrderTo and CheckedOut for Dallas and
their type and status must be CheckedIn and Intranist for New York

In this case my result set would be:
  1245
   1439

How would  I construct the SQL to bring back this result?
cookiejarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
This result from the sample data:
EMPID
1245
1439

from the query below:
SELECT
        EMPID
FROM YOURTABLE
GROUP BY
        EMPID
HAVING SUM(CASE WHEN CITY IN ('New York','Dallas') THEN 1 END) > 1
ORDER BY
        EMPID

Open in new window

visible at: http://sqlfiddle.com/#!4/dfdf9/4
{+ an edit, sorry}
0
cookiejarAuthor Commented:
Suppose I have this as input
1245              Dallas               OrderTo          CheckedOut
1245              New York         CheckedIn    InTransit
1111               Dallas               CheckedIn     Temporary
 1439               Dallas               OrderTo        CheckedOut
  1439             New York         CheckedIn    InTransit
  1880             DeRidder          OrderTo       CheckedOut
   1883            Coushata          CheckedIn   InTransit
 1288              Dallas                ReportsTo          CheckedIn
    1288           New York          CheckedIn    InTransit


With your query I would return 1288 as well but
I only want to return if status is ordersTo and CheckedOut for one City
and CheckedIn and InTransit for the other city.
0
PortletPaulfreelancerCommented:
apologies, not sure how I missed those rules.

Please try:
SELECT
        EMPID
FROM YOURTABLE
WHERE ( CITY = 'New York' AND TYPE = 'CheckedIn' AND STATUS = 'InTransit' )
   OR ( CITY = 'Dallas'   AND TYPE = 'OrderTo'   AND STATUS = 'CheckedOut' )
GROUP BY
        EMPID
HAVING SUM(CASE WHEN CITY IN ('New York','Dallas') THEN 1 END) > 1
ORDER BY
        EMPID
;

Open in new window

(note to self, read the whole question!)
{+ edit} visible at: http://sqlfiddle.com/#!4/8d160/1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

cookiejarAuthor Commented:
Thank you, could you explain to me what this does?

HAVING SUM(CASE WHEN CITY IN ('New York','Dallas') THEN 1 END) > 1
0
PortletPaulfreelancerCommented:
That line is how we locate an EMPID that has BOTH cities

(i.e. If you leave out that line 1288 gets included in the result)

all it is doing is giving each base table row a value of 1 (because every row has to meet the where clause), then we add up those for each EMPID, if the result of that is 1 then the EMPID is only involved in one city not both, If the result is 2 of more then that EMPID has matching records in both cities.

des that help?
0
awking00Information Technology SpecialistCommented:
select x.empid from
(select empid from table where city = 'Dallas'
 and  type = 'OrderTo' and status = 'CheckedOut') x,
(select empid from table where city = 'New York'
 and  type = 'CheckedIn' and status = 'InTransit') y
where x.empid = y.empid
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.