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
Avatar of PortletPaul
PortletPaul
Flag of Australia image

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}
Avatar of cookiejar
cookiejar
Flag of United States of America image

ASKER

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
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of cookiejar
cookiejar
Flag of United States of America image

ASKER

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

HAVING SUM(CASE WHEN CITY IN ('New York','Dallas') THEN 1 END) > 1
Avatar of PortletPaul
PortletPaul
Flag of Australia image

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?
Avatar of awking00
awking00
Flag of United States of America image

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

Oracle is an object-relational database management system. It supports a large number of languages and application development frameworks. Its primary languages are SQL, PL/SQL and Java, but it also includes support for C and C++. Oracle also has its own enterprise modules and application server software.

81K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo