简体   繁体   中英

MYSQL: How to get specific data on table1 and get the latest data on table2 and table3

Table 1: tblemployee

+------------+----------+-----------+-------+-------+
| EmployeeID | LastName | FirstName | ID_SC | ID_ES |
+------------+----------+-----------+-------+-------+
| 100000000  | Vallente | Rhea Mae  | 7     | 2     |
+------------+----------+-----------+-------+-------+
| 100000001  | Margallo | Matt      | 7     | 2     |
+------------+----------+-----------+-------+-------+

Table 2: tblbadge

+---------+------------+--------------+--------+------+
| BadgeNo | EmployeeID | Deactivation | ID_Rem | ID_S |
+---------+------------+--------------+--------+------+
| 1111111 | 100000001  | 2015-01-02   | 9      | 2    |
+---------+------------+--------------+--------+------+
| 2222222 | 100000001  | 2014-01-02   | 9      | 2    |
+---------+------------+--------------+--------+------+

Table 3: tblemployeeactivity

+------------+------------+---------------+-------+
| EmployeeID | ADate      | AttritionDate | ID_AT |
+------------+------------+---------------+-------+
| 100000001  | 2015-01-01 | 2015-01-02    | 1     |
+------------+------------+---------------+-------+
| 100000001  | 2014-01-01 | 2014-01-02    | 1     |
+------------+------------+---------------+-------+

Output Plan:

+------------+----------+-----------+-------+-------+---------+--------------+--------+------+------------+---------------+-------+
| EmployeeID | LastName | FirstName | ID_SC | ID_ES | BadgeNo | Deactivation | ID_Rem | ID_S | ADate      | AttritionDate | ID_AT |
+------------+----------+-----------+-------+-------+---------+--------------+--------+------+-----------------------+----+-------+
| 100000001  | Margallo | Matt      | 7     | 2     | 1111111 | 2015-01-02   | 9      | 2    | 2015-01-01 | 2015-01-02    | 1     |
+------------+----------+-----------+-------+-------+---------+--------------+--------+------+------------+---------------+-------+

Question: How can the lastest BadgeNo and AttritionDate in one query assume that the tblemployee is linked to tblemployeeactivity and same goes with tblemployee and tblbadge however tblbadge and tblemployeeactivity is not link together in SQL.

I have to get the employee's full details on tblemployee and fetch the latest data on each table ( tblbadge & tblemployeeactivity ), Please see output plan, TIA.

PS. Also I have to filter AttritionDate by Year and Month

This query works to generate the desired result:

SELECT e.employeeid, e.lastname, e.firstname, e.id_sc, e.id_es,
       q1.badgeno, q1.deact, q1.id_rem, q1.id_s, 
       q2.adate, q2.attritiondate, q2.id_at
FROM tblemployee e
INNER JOIN 
    (SELECT b.badgeno, b.employeeid, MAX(b.deactivation) AS deact, b.id_rem, b.id_s
     FROM tblbadge b
     GROUP BY b.employeeid) q1
     ON q1.employeeid = e.employeeid
 INNER JOIN
    (SELECT a.employeeid, MAX(a.adate) AS adate, a.attritiondate, a.id_at
     FROM tblemployeeactivity a
     GROUP BY a.employeeid) q2
     ON e.employeeid = q2.employeeid

SQLFiddle: http://sqlfiddle.com/#!9/64a64a/17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM