I have this kind of SQL table with different timestamp
I want to query it so it will take only the last row of each Phase. The output should be like this:
Can someone help?
I have this kind of SQL table with different timestamp
I want to query it so it will take only the last row of each Phase. The output should be like this:
Can someone help?
 
    
     
    
    You can use LEAD to check the next value
WITH NextValues AS (
    SELECT *,
      IsEnd = CASE WHEN LEAD(Phase) OVER (ORDER BY Timestamp) = Phase THEN 0 ELSE 1 END
    FROM YourTable
)
SELECT
  Timestamp,
  Phase,
  greentime
FROM NextValues t
WHERE IsEnd = 1;
