根据mysql子集中另一列的最大值从子集中选择列

时间:2017-04-22 19:52:53

标签: mysql mysql-5.7

生物学家和mySQL(版本5.7.13)在这里初学者,我目前面临的任务是我无法解决这个问题。我有一张表记录了个人的踪迹和时间,数据摘录如下:

Table "tblSightings"
+---------------+---------+-----------+---------------------+
| id_individual | project | id_survey | Surveydatetime      |
+---------------+---------+-----------+---------------------+
| A             |       1 | S1        | 2016-11-18 15:54:00 |
| B             |       1 | S1        | 2016-11-18 15:54:00 |
| C             |       1 | S1        | 2016-11-18 15:54:00 |
| A             |       1 | S2        | 2016-11-06 13:33:00 |
| B             |       1 | S2        | 2016-11-06 13:33:00 |
| X             |       1 | S2        | 2016-11-06 13:33:00 |
| A             |       2 | S3        | 2015-05-01 12:48:00 |
+---------------+---------+-----------+---------------------+

我想要做的是创建一个查询,列出最近的个人目标(id_individual +项目的最高时间表)以及相应的id_survey以及在该目击期间与其一起看到的所有其他人( GROUP_CONCAT(id_individual))。基于此处示例数据的期望结果将是:

+---------------+---------+---------------+------------+---------------------+
| id_individual | project | id_survey     | associates | latest              |
+---------------+---------+---------------+------------+---------------------+
| A             |       1 | S1            | B C        | 2016-11-18 15:54:00 |
| B             |       1 | S1            | A C        | 2016-11-18 15:54:00 |
| C             |       1 | S1            | A B        | 2016-11-18 15:54:00 |
| X             |       1 | S2            | A B        | 2016-11-06 13:33:00 |
| A             |       2 | S3            |            | 2015-05-01 12:48:00 |
+---------------+---------+---------------+------------+---------------------+

我确实知道如何使用

为每个人获取最新的Surveydatetime
SELECT 
id_individual, 
project, 
MAX(Surveydatetime) AS latest 
FROM tblSightings 
GROUP BY id_individual, project; 

但我无法弄清楚如何为“最新”列获取相应的“id_survey”,因此也无法弄清楚如何从所需结果中的关联列的GROUP_CONCAT中获取所有id_individuals。当我在SELECT中包含id_survey时,它不起作用,因为我也必须将它放在GROUP BY中,从而导致每个人再次出现多行。到目前为止,我发现的“max of subsets”的大多数答案都是INNER JOIN,其中包含一个SELECT语句,但我根本无法使用它...

任何帮助非常感谢!谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个:

Select
        t2.id_individual, t2.project, t2.survey id_survey,
        (
            Select GROUP_CONCAT(tt.id_individual)
            From tblsightings tt
            Where tt.project = t2.project and tt.id_survey = t2.survey and tt.id_individual <> t2.id_individual
        ) associates,
        t2.maxdate latest
From
(
      Select t1.project, t1.id_individual, maxdate,
            (
                Select id_survey
                From tblsightings tt
                Where tt.project = t1.project and tt.id_individual = t1.id_individual and tt.surveydatetime = t1.maxdate
            ) survey
      From 
      (
          Select project, id_individual, max(surveydatetime) maxdate
          From tblsightings t1
          Group by project, id_individual
      ) t1
) t2
Order by t2.project, t2.id_individual

我使用的数据:

CREATE TABLE tblsightings 
(
  id_individual varchar(100),
  surveydatetime varchar(100),
  id_survey varchar(100),
  project varchar(100)

  );

INSERT INTO tblsightings (id_individual,surveydatetime,id_survey,project) VALUES ("A","2016-11-18 15:54:00","S1","1");
INSERT INTO tblsightings (id_individual,surveydatetime,id_survey,project) VALUES ("B","2016-11-18 15:54:00","S1","1");
INSERT INTO tblsightings (id_individual,surveydatetime,id_survey,project) VALUES ("C","2016-11-18 15:54:00","S1","1");
INSERT INTO tblsightings (id_individual,surveydatetime,id_survey,project) VALUES ("A","2016-11-06 13:33:00","S2","1");
INSERT INTO tblsightings (id_individual,surveydatetime,id_survey,project) VALUES ("B","2016-11-06 13:33:00","S2","1");
INSERT INTO tblsightings (id_individual,surveydatetime,id_survey,project) VALUES ("X","2016-11-06 13:33:00","S2","1");
INSERT INTO tblsightings (id_individual,surveydatetime,id_survey,project) VALUES ("A","2015-05-01 12:48:00","S3","2");

答案 1 :(得分:0)

以下是编写此查询的一种方法:

SELECT t1.id_individual, t1.project, ts.id_survey, t1.latest,
GROUP_CONCAT(t2.id_individual) AS associates

FROM tblSightings ts
    INNER JOIN
    ( SELECT 
            id_individual, 
            project, MAX(Surveydatetime) AS latest 
        FROM tblSightings 
        GROUP BY id_individual, project
    ) t1
        ON t1.id_individual = ts.id_individual
        AND t1.project = ts.project
        AND t1.latest = ts.Surveydatetime

    LEFT JOIN tblSightings t2
        ON ts.id_survey = t2.id_survey
        AND ts.project = t2.project
        AND t1.latest = t2.Surveydatetime
        AND t1.id_individual != t2.id_individual

    GROUP BY t1.id_individual, t1.project, ts.id_survey, t1.latest
    ORDER BY t1.latest DESC, t1.project, t1.id_individual, ts.id_survey;


说明:

为了达到给定的结果格式,我们需要JOIN相同的表三次。第一个是INNER JOIN,用于获取每个项目每个人最高时间戳记录的id_survey。第二个是确定给定个人是否有任何员工。由于可能根本没有任何合作伙伴(如S3所示),因此我们在此处使用LEFT JOIN。我们还确定此LEFT JOIN仅适用于与正在处理记录的个人不同的id_individual,但他们是同一项目和调查的一部分。< / p>


Demo link