基于时间戳MAX的SQL内连接

时间:2015-12-12 03:28:33

标签: sql sqlite max inner-join where-clause

修改一次

修改两次:除报告外,其余9个表格的标题总是被调用"什么"。

我有大约10个具有以下结构的表:

reports (165k rows)
+-----------+-----------+
| identifier| category  | 
+-----------+-----------+
| 1         | fixed     |
| 2         | wontfix   |
| 3         | fixed     |
| 4         | invalid   | 
| 5         | later     | 
| 6         | wontfix   | 
| 7         | duplicate | 
| 8         | later     | 
| 9         | wontfix   | 
+-----------+-----------+   
 status (300k rows, all identifiers from reports come up at least once)
+-----------+-----------+----------+
| identifier| time      | what     |
+-----------+-----------+----------+
| 1         | 12        | RESOLVED |
| 1         | 9         | NEW      |
| 2         | 7         | ASSIGNED |
| 3         | 10        | RESOLVED |
| 5         | 4         | REOPEN   |
| 7         | 9         | ASSIGNED |
| 4         | 9         | ASSIGNED |
| 7         | 11        | RESOLVED |
| 8         | 3         | NEW      |
| 4         | 3         | NEW      |
| 7         | 6         | NEW      |
+-----------+-----------+----------+

 priority (300k rows, all identifiers from reports come up at least once)
+-----------+-----------+----------+
| identifier| time      | what     |
+-----------+-----------+----------+
| 3         | 12        | LOW      |
| 1         | 9         | LOW      |
| 9         | 2         | HIGH     |
| 8         | 7         | HIGH     |
| 3         | 10        | HIGH     |
| 5         | 4         | MEDIUM   |
| 4         | 9         | MEDIUM   |
| 4         | 3         | LOW      |
| 7         | 9         | LOW      |
| 7         | 11        | HIGH     |
| 8         | 3         | LOW      |
| 6         | 12        | MEDIUM   |
| 7         | 6         | LOW      |
| 6         | 9         | HIGH     |
| 2         | 6         | HIGH     |
| 2         | 1         | LOW      |
+-----------+-----------+----------+

我需要的是:

 reportsfinal (165k rows)
+-----------+-----------+--------------+------------+
| identifier| category  | what11       |  what22    |
+-----------+-----------+--------------+------------+
| 1         | fixed     | RESOLVED     | LOW        |
| 2         | wontfix   | ASSIGNED     | HIGH       |
| 3         | fixed     | RESOLVED     | LOW        |
| 4         | invalid   | ASSIGNED     | MEDIUM     |
| 5         | later     | REOPEN       | MEDIUM     |
| 6         | wontfix   |              | MEDIUM     |
| 7         | duplicate | RESOLVED     | HIGH       |
| 8         | later     | NEW          | HIGH       |
| 9         | wontifx   |              | HIGH       |
+-----------+-----------+--------------+------------+

也就是说,reports(在查询= reportsfinal之后)作为基表,我必须从其他9个表中添加一列或两列。 identifier是关键,但在某些表中,identifier会多次出现。在这些情况下,我想只使用时间最长的条目。 我尝试了几个查询,但没有一个查询。如果可能的话,我想运行一个查询,用这种方法从其他9个表中获取不同的列。

根据以下答案我尝试了什么:



select  T.identifier,
        T.category,
        t.what AS what11,
        t.what AS what22 from (
     select R.identifier,
     R.category,
     COALESCE(S.what,'NA')what,
     COALESCE(P.what,'NA')what,
     ROW_NUMBER()OVER(partition by R.identifier,R.category ORDER by (select null))RN
     from reports R 
     LEFT JOIN bugstatus S
     ON S.identifier = R.identifier
     LEFT JOIN priority P
     ON P.identifier = s.identifier

     GROUP BY R.identifier,R.category,S.what,P.what)T
     Where T.RN = 1
     ORDER BY T.identifier;




这给出了错误:



Error: near "(": syntax error.




3 个答案:

答案 0 :(得分:1)

对于每个关联的表,只需使用基于子查询的谓词来标识特定的时间戳...

单字母标记r,s和p分别是表格报告,状态和优先级的别名

Select r.Identifier, r.category,
   coalesce(s.what, 'NA') status,
   coalesce(p.what, 'NA') priority
From reports r
  left join status s
     on s.identifier = r.identifier
        and s.time =
           (Select max(time) from status 
            where identifier = r.identifier)
  left join priority p
     on p.identifier = r.identifier
        and p.time =
           (Select max(time) from priority 
            where identifier = r.identifier);

问题:为什么要将列从Statuspriority重命名为What?您也可以命名somethingdatainformation。至少原始名称(statusprio)传达了某些信息。What这个词毫无意义。

请注意。我对what11what12的别名的编辑进行了反转(取消),因为这些名称毫无意义。

答案 1 :(得分:1)

基本上,您需要在选择列表中使用相关子查询。

从臀部开始,像是:

Select a.Identifier
,a.Category
,(select process
    from status where status.identifier = a.Identifer order by time desc limit 1) Process
,(select prio
    from priority where priorty.identifier = a.Identifer order by time desc limit 1) prio
From Reports a

答案 2 :(得分:0)

使用Row_number基于您的假设数据

field_data_field_selected_players