Solved

Subquery in Oracle SQL Statement

Posted on 2013-06-17
7
422 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle encryption 12 48
Where on a calculated field 1 21
SQL group by query to return records with highest value 6 24
How to structure query with count aggregate 4 13
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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 explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

840 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