Raja_23
05-02-2023Data Storage
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 max event number row only.
how to achieve this.
actual table:
shipment key | event no | max event no |
11423 | 1 | 5 |
11423 | 2 | 5 |
11423 | 3 | 5 |
11423 | 4 | 5 |
11423 | 5 | 5 |
11455 | 1 | 3 |
11455 | 2 | 3 |
11455 | 3 | 3 |
Expected output:
shipment key | event no | max event no |
11423 | 5 | 5 |
11455 | 3 | 3 |
Not only shipment key I have some other categories also there. I need max event no= event no.
raja.
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