I am trying to write a query that would select data from multiple lines depending if there is a newer value. This has been giving me headaches for a long time already. The table is structured this way:
UniqID ProblemID DateTime Status PersonID ResponsibleID ActionID Comment Deadline PartID
15589 15589 01/16 12:11 0 25 48 12 bla1 01/16 12:11 T9865
15592 15589 01/16 12:25 1 48 48 105 bla2 null null
15601 15589 01/16 13:08 3 56 null 195 bla3 null N2654
15641 15589 01/17 18:02 3 11 23 35 null 01/18 15:00 null
15705 15589 01/18 10:24 5 23 null 255 bla4 null null
This is a mistake log and I need to perform several slightly different queries on this data.
- e.g. if person 48 was involved with this mistake (15589) as
PersonIDor asResponsibleID.
This query would have to return
ProblemID (first)DateTime (last)Status (first)Person (last)Responsible (last)action (first)Comment (last)deadline (last)PartID
15589 01/16 12:11 5 25 23 255 bla1 01/18 15:00 N2654
- e.g show all mistakes for the last 24hours
- e.g show all mistakes where
PartIDT9865 was involved
I have made several attempts at this query, but the best that came out was a query that selects all stuff from the last line or the first one. I am actually struggling to figure out how to select the (last)ResponsibleID if it is not contained in the last line. The same for other columns obviously. The only solution I can think of right now is a simple select that would return me all the rows and just filter that necessary data in a foreach loop with PHP. Because selecting different values in multiple lines is just beyond my skill 🙂
Thanks in advance
The only solution I can think of is to repeat the conditions in subselects. I see no other way to get to the first/last column values that are
NOT NULL.SQL Statement
Test script