Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

using max in where clause

2886750Nov 2 2015 — edited Nov 3 2015

Hello All,

I have data that looks like this.

PLAN  PLAN_INSTANCE DATE_START DATE_END TOTALS

1007  1             20150101   20151231 10

1007  2             20140101   20141231 25

1007  3             20130101   20131231 12         

1008  1             20150101   20151231 22

1008  2             20140101   20141231 9

I would like run a query that displays one row per plan based on max(date_start).

i have tried

select plan, t.date_start, date_end, totals

from t_totals t

where t.date_start= (select max(date_start)

                          from t_totals

                          where date_start= t.date_start);

but this doesn't seem to work as i get all the 5 rows instead of 2 rows.

Can you suggest me how i can modify my query?

Here is the table information

CREATE TABLE T_TOTALS

(

  PLAN                    NUMBER(9)             NOT NULL,

  PLAN_INSTANCE           NUMBER(4)             NOT NULL,

  DATE_START              NUMBER(8)             NOT NULL,

  DATE_END                NUMBER(8)             NOT NULL,

  TOTALS                  NUMBER(9)             NOT NULL,

);

CREATE UNIQUE INDEX I_TOTALS ON T_TOTALS

(SAK_PUB_HLTH, SAK_SHORT);

CREATE INDEX X_TOTALS_1 ON T_TOTALS

(SAK_PUB_HLTH, DTE_EFFECTIVE);

Thanks in advance,

sqlamateur

This post has been answered by Frank Kulash on Nov 2 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 1 2015
Added on Nov 2 2015
6 comments
852 views