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 and group by with case when

SezginCebiApr 12 2017 — edited Apr 13 2017

Hi

I have a table like in below. I want to get max value at col6 using col1 as group by when it is 0. My sql syntax like in below but didn't work. I have no idea about using max and group by function at case/when  area if this is possible please suggest me what i am missing

Thank you

Database Version

11.2.0.3

Query

SELECT

        col1

       ,col2

       ,col3

       ,col4

       ,col5

       , case when 0 

    then (select max(col6) from MYTABLE group by col1)

    else col6

    end as  col6  

       ,[col7]

   FROM MYTABLE;

Sample Data

create table mytable (col1 number,col2 number,col3  number,col4 number,col5 number,col6 number,col7 number);

insert into mytable values (10639,2,2017,1,0.823,0,0.823);

insert into mytable values (10639,3,2017,1,0.798,0,0.798);

insert into mytable values (10639,4,2017,2,0.825,0.798,0.825);

insert into mytable values (10876,1,2017,1,0.826,0,0.826);

insert into mytable values (10876,2,2017,1,0.798,0,0.798);

insert into mytable values (10876,3,2017,1,0.828,0,0.828);

insert into mytable values (10876,4,2017,2,1,0.825,1);

insert into mytable values (11471,1,2017,1,0.792,0,0.792);

insert into mytable values (11471,2,2017,1,0.793,0,0.793);

insert into mytable values (11471,3,2017,1,0.794,0,0.794);

insert into mytable values (11471,4,2017,2,0.798,0.798,0.798);

insert into mytable values (11583,1,2017,1,0.796,0,0.796);

insert into mytable values (11583,2,2017,1,0.797,0,0.797);

insert into mytable values (11583,3,2017,1,0.798,0,0.798);

insert into mytable values (11583,4,2017,2,0.799,0.798,0.799);

insert into mytable values (11650,1,2017,1,0.826,0,0.826);

insert into mytable values (11650,2,2017,1,0.827,0,0.827);

insert into mytable values (11650,3,2017,1,0.828,0,0.828);

insert into mytable values (11650,4,2017,2,0.798,0.798,0.798);

Expected Output

  

col1col2col3col4col5col6col7
106392201710.8230.7980.823
106393201710.7980.7980.798
106394201720.8250.7980.825
108761201710.8260.8250.826
108762201710.7980.8250.798
108763201710.8280.8250.828
1087642017210.8251
114711201710.7920.7980.792
114712201710.7930.7980.793
114713201710.7940.7980.794
114714201720.7980.7980.798
115831201710.7960.7980.796
115832201710.7970.7980.797
115833201710.7980.7980.798
115834201720.7990.7980.799
116501201710.8260.7980.826
116502201710.8270.7980.827
116503201710.8280.7980.828
116504201720.7980.7980.798
This post has been answered by John Spencer on Apr 12 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 11 2017
Added on Apr 12 2017
8 comments
1,028 views