vendredi 11 décembre 2015

SQL Inner Join based on MAX of timestamp

I have about 10 tables with the following structure:

reports
+-----------+-----------+
| identifier| category  | 
+-----------+-----------+
| 1         | fixed     |
| 2         | wontfix   |
| 3         | fixed     |
| 4         | invalid   | 
| 5         | later     | 
| 6         | wontfix   | 
| 7         | duplicate | 
| 8         | later     | 
| 9         | wontfix   | 
+-----------+-----------+   
 status
+-----------+-----------+----------+
| identifier| time      | process  |
+-----------+-----------+----------+
| 1         | 12        | RESOLVED |
| 1         | 9         | NEW      |
| 2         | 7         | ASSIGNED |
| 3         | 10        | RESOLVED |
| 5         | 4         | REOPEN   |
| 7         | 9         | ASSIGNED |
| 7         | 11        | RESOLVED |
| 8         | 3         | NEW      |
| 7         | 6         | NEW      |
+-----------+-----------+----------+

 priority
+-----------+-----------+----------+
| identifier| time      | prio     |
+-----------+-----------+----------+
| 3         | 12        | LOW      |
| 1         | 9         | LOW      |
| 8         | 7         | HIGH     |
| 3         | 10        | HIGH     |
| 5         | 4         | REOPEN   |
| 7         | 9         | LOW      |
| 7         | 11        | HIGH     |
| 8         | 3         | LOW      |
| 7         | 6         | LOW      |
+-----------+-----------+----------+

What I need is:

 reportsfinal
+-----------+-----------+----------+------------+
| identifier| category  | process  |  prio      |
+-----------+-----------+----------+------------+
| 1         | fixed     | RESOLVED | LOW        |
| 2         | wontfix   | ASSIGNED | NA         |
| 3         | fixed     | RESOLVED | LOW        |
| 4         | invalid   | NA       | NA         |
| 5         | later     | REOPEN   | REOPEN     |
| 6         | wontfix   | NA       | NA         |
| 7         | duplicate | RESOLVED | HIGH       |
| 8         | later     | NEW      | HIGH       |
| 9         | wontifx   | NA       | NA         |
+-----------+-----------+----------+------------+

That is, reports (after query = reportsfinal) serves as the basis table and I have to add one or two columns from 9 other tables. The identifier is the key, but in some tables, the identifier comes up multiple times. In these cases I want to use the entry with the highest time only. I tried several queries, but none of them worked. If possible, I want to run one query to get different columns from the 9 other tables with this approach.

Aucun commentaire:

Enregistrer un commentaire