Forum Discussion

Raja_23's avatar
Raja_23
Data Storage
05-02-2023
Solved

need implement condtion in table

I have a table widget it's have event number 1,2,3,4,5,6,7 with some shipment keys. I created a max event number (new column in new table) with grouping of shipment key customer want to see only ma...
  • HamzaJ's avatar
    HamzaJ
    05-03-2023

    Hey Raja_23 

    You could do this:

    select
    a.*
    from vwShipmentStatus a
    join (
    select [ShipmentKey],max([ShipmentEventNumber]) as MaxShipmentEvent_NR
    from vwShipmentStatus 
    GROUP BY [ShipmentKey] ) b on a.[ShipmentKey] = b.[ShipmentKey] and a.[ShipmentEventNumber] = b.MaxShipmentEvent_NR

    which will give you the result you are looking for (e.g. expected output).

    If you want more flexibility you could also do 

    select
    a.*, 
    b.MaxShipmentEvent_NR,
    case 
    when a.[ShipmentEventNumber] = b.MaxShipmentEvent_NR then 'Yes'
    else 'No' 
    end as Max_event
    from vwShipmentStatus a
    join (
    select [ShipmentKey],max([ShipmentEventNumber]) as MaxShipmentEvent_NR
    from vwShipmentStatus 
    GROUP BY [ShipmentKey] ) b on a.[ShipmentKey] = b.[ShipmentKey] 

    This will give you the total table with a flag (Max_event). You can filter on this to only show max_event or to show everything

    Hamza