Please can you help me to query the last LINE row for each OPPRID?
I'm using MS SQL and this query is not working. Can't figure out way the Max(line) result is not returning the maximum value (for OpprId = 2 it should be 4)
SELECT OpprId, Line, Step_Id
FROM mytable
WHERE OpprId IN (
SELECT MAX(Line)
FROM mytable
GROUP BY OpprId
);
Table:
+--------+------+-------+
| OpprId | Line | Step |
+--------+------+-------+
| 2 | 0 | Phone |
+--------+------+-------+
| 2 | 1 | SMS |
+--------+------+-------+
| 2 | 2 | Quote |
+--------+------+-------+
| 2 | 4 | Order |
+--------+------+-------+
| 3 | 0 | Phone |
+--------+------+-------+
| 3 | 1 | Email |
+--------+------+-------+
| 4 | 0 | Phone |
+--------+------+-------+
| 4 | 1 | Quote |
+--------+------+-------+
| 4 | 2 | Email |
+--------+------+-------+
Expected result:
+--------+------+---------+
| OpprId | Line | Step_Id |
+--------+------+---------+
| 2 | 4 | Order |
+--------+------+---------+
| 3 | 1 | Email |
+--------+------+---------+
| 4 | 3 | Email |
+--------+------+---------+