Skip to content
Advertisement

Excluding rows in a SQL query based on values in another row while preserving multiple outputs of a single part ID

I have machine output data in the form:

and I need to generate a new table by removing some rows:

The machine sometimes collects multiple VALUEs for each run, but I only need the last one (it’s cumulative). However, I may have multiple runs of the same ID per shift, and it’s not impossible to have 2 consecutive runs of the same ID.

Is it possible with SQL to filter out all rows where ID of a row is equal to the ID of the row above it, only if the VALUE is greater than the VALUE of the row above it?

There’s some similar Qs posted here but they all result in grouping rows and taking a max value, but then I’d only capture one run per ID for each time period.

Advertisement

Answer

You seem to want the rows where the id changes and the value increases:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement