0

我在这里有这个可怕的查询:

SELECT t.technician_id AS techID, CONCAT(t.first_name, ' ', t.last_name) AS technicianName, h.date, p.name
FROM technician t
JOIN technician_date td ON t.technician_id = td.technician_id
JOIN hours_input_on_date h ON h.id = td.hours_input_on_date_id
JOIN technician_date_project tdp ON tdp.technician_date_id = td.technician_date_id
JOIN projects p ON p.id = tdp.project_id
JOIN(
    SELECT h.date, p.name
    FROM technician t
    JOIN technician_date td ON t.technician_id = td.technician_id
    JOIN hours_input_on_date h ON h.id = td.hours_input_on_date_id
    JOIN technician_date_project tdp ON tdp.technician_date_id = td.technician_date_id
    JOIN projects p ON p.id = tdp.project_id
    GROUP BY h.date, p.name
    HAVING COUNT(*) > 1) temp ON h.date = temp.date AND p.name = temp.name
JOIN(
    SELECT MAX(date) AS latestDate, name
    FROM(
        SELECT h.date, p.name
        FROM technician t
        JOIN technician_date td ON t.technician_id = td.technician_id
        JOIN hours_input_on_date h ON h.id = td.hours_input_on_date_id
        JOIN technician_date_project tdp ON tdp.technician_date_id = td.technician_date_id
        JOIN projects p ON p.id = tdp.project_id
        GROUP BY h.date, p.name
        HAVING COUNT(*) > 1) t
    GROUP BY t.name) temp1 ON temp1.latestDate = temp.date AND temp1.name = temp.name;

当不止一名技术人员在同一天从事同一个项目时,这样做的目的是让技术人员和项目得到解决。请参阅我之前的问题以进行澄清。这就是第一组连接和第一个连接子查询完成的工作。

现在,我只想获得在同一天从事一个项目的最新技术人员。我添加了另一个自我加入,它从多个技术人员在同一天工作的项目组中获取最大日期。是否有更有效的方法来删除最后一个JOIN块并仍将分组限制在最新日期,或者自加入是唯一的方法?

为了更清楚地说明,假设我取出了最后一个JOIN块,我会看到如下内容:

| tech |    date    | project |
+------+------------+---------+
| Adam | 2014-12-04 |  Math   |
| John | 2014-12-04 |  Math   |
| Jane | 2014-12-04 |  Math   |
| Adam | 2014-12-05 |  Math   |
| John | 2014-12-05 |  Math   |

我只想看到最后两行,因为它是该项目的最新组,但我能比再次加入我的所有表更有效吗?

编辑

这是一个有助于演示差异的SQL Fiddle 。

4

2 回答 2

1

你可以在你的子查询中嵌套一个“max(date) group by name”:

SELECT t.technician_id AS techID, CONCAT(t.first_name, ' ', t.last_name) AS technicianName, h.date, p.name
FROM technician t
JOIN technician_date td ON t.technician_id = td.technician_id
JOIN hours_input_on_date h ON h.id = td.hours_input_on_date_id
JOIN technician_date_project tdp ON tdp.technician_date_id = td.technician_date_id
JOIN projects p ON p.id = tdp.project_id
JOIN(

    SELECT max(date) date, name from (

    SELECT h.date, p.name
    FROM technician t
    JOIN technician_date td ON t.technician_id = td.technician_id
    JOIN hours_input_on_date h ON h.id = td.hours_input_on_date_id
    JOIN technician_date_project tdp ON tdp.technician_date_id = td.technician_date_id
    JOIN projects p ON p.id = tdp.project_id
    GROUP BY h.date, p.name
    HAVING COUNT(*) > 1

    ) subtemp
    group by name

) temp ON h.date = temp.date AND p.name = temp.name;
于 2014-12-09T10:17:05.607 回答
0

通过像这样将 MAX 添加到 h.date 周围的第一个查询中

SELECT t.technician_id AS techID, CONCAT(t.first_name, ' ', t.last_name) AS technicianName,          MAX(h.date), p.name
FROM technician t
JOIN technician_date td ON t.technician_id = td.technician_id
JOIN hours_input_on_date h ON h.id = td.hours_input_on_date_id
JOIN technician_date_project tdp ON tdp.technician_date_id = td.technician_date_id
JOIN projects p ON p.id = tdp.project_id
JOIN(
SELECT h.date, p.name
FROM technician t
JOIN technician_date td ON t.technician_id = td.technician_id
JOIN hours_input_on_date h ON h.id = td.hours_input_on_date_id
JOIN technician_date_project tdp ON tdp.technician_date_id = td.technician_date_id
JOIN projects p ON p.id = tdp.project_id
GROUP BY h.date, p.name
HAVING COUNT(*) > 1) temp ON h.date = temp.date AND p.name = temp.name;

您将在第二个查询中获得所需的数据。

于 2014-12-05T16:57:26.883 回答