Skip to content
Advertisement

Mark values with diffreent tag in sql

I have one endpoint that is 7. I would like to few numbers 40,35,30,26,22,18,12 mark as completed.(This is an example. The value may be different) and few numbers 13,17,21,27,32,38,43 mark as pending. (This is an example. The value may be different) Can we achieve by SQL statement? for number details, please find the image.

enter image description here

Advertisement

Answer

If your DBMS supports Windowed Aggregates:

with cte as
 ( select ID, point,
      -- find all rows after the latest 7 row
      sum(case when point = 7 then 1 end)
      over (order by ID DESC) as cumsum
   from tab
 )
select ID, point,
   case when point = 7 then 'endpoint'
        when cumsum is null then 'pending' -- no 7 after those IDs
        else 'completed'
   end 
from cte
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement