Avatar of cookiejar
cookiejarFlag for United States of America asked on

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?
Oracle Database

Avatar of undefined
Last Comment
awking00

8/22/2022 - Mon
PortletPaul

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}
ASKER
cookiejar

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.
ASKER CERTIFIED SOLUTION
PortletPaul

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
cookiejar

Thank you, could you explain to me what this does?

HAVING SUM(CASE WHEN CITY IN ('New York','Dallas') THEN 1 END) > 1
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
PortletPaul

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?
awking00

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