let’s say I have the following elements:
Actions table
id type date
----------- ---------- ----------
1 start 2012-01-01 10:00:00
2 suspend 2012-01-01 11:00:00
3 resume 2012-01-01 12:00:00
4 suspend 2012-01-01 13:00:00
5 resume 2012-01-01 14:00:00
6 stop 2012-01-01 15:00:00
7 refill 2012-01-01 16:00:00
8 stop 2012-01-01 17:00:00
Association table
id actionID elementID
----- ----------- -----------
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 6 1
7 7 1
8 8 1
Elements table
id name
----- -----------
1 my element
So basically I have an element, its status evolves according to some user triggered actions (action table), thoses actions can be shared through many elements (that’s why I have an association table).
What I’d like to retrieve in an SQL query its:
ElementID, start Action ID, last action ID(start action is always linked to an element)
is it possible without writing multiple sql queries ? i’m looking for the most performant query since it’ll run on a table with many millions of entries (the association table & action table)
Thanks
1 Answer