HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Get if field has max value and display value

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
fieldvaluehasgetmaxanddisplay

Problem

I need to display Y or N when displaying rows in a report based on the max value for them on their version. If they have multiple versions all of them will display 'N' but the max (last version) will display 'Y'.

I have tried with case when and max but always I'm getting 'Y'

case when version = (select max(version) from t_proj_f where version = t.version) then 'Y' else 'N' end Is_last_version


Here is fiddle in case anybody can take a look
http://sqlfiddle.com/#!4/813501/4

I'm new on oracle sql any help is appreciated.

Solution

I think this will give you want you want:

select project_id,
        version,
        stop,
        t_id,
        status,
        mp,
        case when rowno = 1 then 'Y' else 'N' end is_last_version
        from (
              select project_id,
                     version,
                     stop,
                     t_id,
                     status,
                     mp,
                     row_number() over (partition by projecT_id order by version desc) 
                        as rowno
              from t_proj_f t
              order by project_id)

Code Snippets

select project_id,
        version,
        stop,
        t_id,
        status,
        mp,
        case when rowno = 1 then 'Y' else 'N' end is_last_version
        from (
              select project_id,
                     version,
                     stop,
                     t_id,
                     status,
                     mp,
                     row_number() over (partition by projecT_id order by version desc) 
                        as rowno
              from t_proj_f t
              order by project_id)

Context

StackExchange Database Administrators Q#271450, answer score: 4

Revisions (0)

No revisions yet.