Solved

Subquery in Oracle SQL Statement

Posted on 2013-06-17
7
423 Views
Last Modified: 2013-06-18
Experts,

Does anyone know how to write the following query in Oracle.

i need to select all records where DateA is = 01/31/2013 for MemberA and Member A has no records where DATEA is  greater than 01/31/2013?
0
Comment
Question by:morinia
7 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39254301
I'm thinking this can be simplified but this is off the top of my head:

select * from table where member='MemberA' and dateA = to_date('01/31/2013','MM/DD/YYYY')
and not exists (select 1 from table where member='MemberA' and dateA >= to_date('01/31/2013','MM/DD/YYYY'))
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39254342
Now that I've been trying to simplify it, I'm not sure I'm following.

If memberA has a record with 1/31/2013 and records > 1/31/2013, are you wanting no rows returned?

Can you post some sample data and expected results?
0
 

Author Comment

by:morinia
ID: 39254362
File A
    MEMBER      DATEA
   MEMBERA     1/31/2013
   MEMBERB    1/31/2013
   MEMBERB    2/01/2013
   MEMBERC    1/13/2013
   MEMBERD    1/13/2013
   MEMBERD    2/01/3013


With this data, I would like only MEMBERA 1/31/2013 and MEMBERC returned

I don't want to select  MEMBERB AND MEMBERC because they have records with date = 2/01/2013 which is greater than 1/31/2013
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 74

Expert Comment

by:sdstuber
ID: 39254386
Not as straightforward, but possibly more efficient since it only needs to read the table once
test on your own system to confirm though


SELECT *
  FROM (SELECT t.*
               , MAX(CASE WHEN datea >= TO_DATE('01/31/2013', 'MM/DD/YYYY') THEN 'Y' END) OVER (partition by member) rejected
          FROM yourtable t   )
 WHERE datea = TO_DATE('01/31/2013', 'MM/DD/YYYY') AND rejected IS NULL
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39254568
select *
  from table a
 where a.date = to_date('01/31/2013', 'mm/dd/yyyy')
   and not exists (select 'X'
          from table b
         where b.member = a.member
           and b.date > a.date);
0
 
LVL 6

Expert Comment

by:ianmills2002
ID: 39255104
Another variation you can try is as follows

select member, min(datea), max(datea)
from yourtable
where  datea >= TO_DATE('01/31/2013', 'MM/DD/YYYY')
group by member
having min (datea) = TO_DATE('01/31/2013', 'MM/DD/YYYY')
 and min(datea) = max(datea)
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39255384
ID: 39254386 needs a small tweak remove = from >=
 , MAX(CASE WHEN datea > TO_DATE('01/31/2013', 'MM/DD/YYYY') THEN 'Y' END) OVER

with that applied 3 existing variants all returning MEMBERA only
(I think there is an inconsistency in wording of the expected results by the way)

all those, plus another below are available for comparison here
http://sqlfiddle.com/#!4/5604f/2

I think ianmills marginally has the lowest cost - but sqlfiddle results don't account for indexes etc. and volume is very low of course

:) my variant
SELECT
  member
, datea
FROM (
      SELECT
        member
      , datea
      , row_number() over (partition BY member ORDER BY datea DESC) AS row_ref
      FROM table1
     )
WHERE row_ref = 1
AND datea = TO_DATE('2013-01-31', 'YYYY-MM-DD')
;

Open in new window

ps: I don't natively use MM/DD/YYYY just seems weird to me
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

730 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