I'm trying to get a valua based on a dynamic lag function, which I won't get. I have a table that looks like this, and what I want is the value from product from the Serial Number that is 1 higher then my current Serial Number
+--------+---------+--------+------------------------+----------+
| Client | Product | Ser_No | Product Ser_No+1 I get | Expected |
+--------+---------+--------+------------------------+----------+
| 1 | A | 1 | B | B |
+--------+---------+--------+------------------------+----------+
| 1 | B | 2 | C | C |
+--------+---------+--------+------------------------+----------+
| 1 | C | 3 | D | F |
+--------+---------+--------+------------------------+----------+
| 1 | D | 3 | E | F |
+--------+---------+--------+------------------------+----------+
| 1 | E | 3 | F | F |
+--------+---------+--------+------------------------+----------+
| 1 | F | 4 | null | null |
+--------+---------+--------+------------------------+----------+
| 2 | D | 1 | E | E |
+--------+---------+--------+------------------------+----------+
| 2 | E | 2 | F | G |
+--------+---------+--------+------------------------+----------+
| 2 | F | 2 | G | G |
+--------+---------+--------+------------------------+----------+
| 2 | G | 3 | H | H |
+--------+---------+--------+------------------------+----------+
| 2 | H | 4 | I | I |
+--------+---------+--------+------------------------+----------+
| 2 | I | 5 | null | null |
+--------+---------+--------+------------------------+----------+
The only thing I can come up with is the Lag(product, 1), but that is not exactly what I want right now. Any ideas?
This is what I do right now:
LEAD(Product, 1) OVER(PARTITION BY Cliƫnt ORDER BY Ser_No ASC)
But the problem is rows 3, 4 and 8. This is where i need the productcode not from the next row but from the next Ser_No
over()part of the clause is suspiciously missing from your question. It should belag(product,1) over (partition by product order by ser_no asc). Is that what you have done? - George Menoutis