Solved

Subquery in Oracle SQL Statement

Posted on 2013-06-17
7
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 49

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

696 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