I'll explain my scenario. Mysql 5.7
I have 2 table: registry and movement. This two table have IDMovement in common so i can join them
My problem: I need to GROUP BY the Registry column (is varchar column), i need to check the MAX IDMovement. In this row, there is another column called IDCausal. If on the MAX IDMovement the IDCasual is different to 21, then skip. Else return the row.
I'll make an example of what i have done:
SELECT
m.IDMovement,
mo.IDCausal,
m.Registry
FROM
registry m
JOIN movement mo ON m.IDMovement = mo.IDMovement
WHERE
m.Registry = "2SST0160"
P.S. The WHERE is just an example, i need to do the query for every Registry.
The return values for the registry 2SST0160 are:
IDMovement IDCausal Registry
5550 21 2SST0160
9817 5 2SST0160
In the example, the MAX IDMovement is 9817 but the IDCausal is 5, so the expected result of the query in this case is a NOTHING. If the IDCausal was 21, the expected result is only the row with 21. For every Registry, i'll expect ZERO or ONE row as result for every registry.
I'll hope i was clear, thanks for the help!