Solved

Subquery in Oracle SQL Statement

Posted on 2013-06-17
7
418 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 76

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 76

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to recover a database from a user managed backup

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now