Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于最大时间戳的SQL内部联接_Sql_Sqlite_Max_Inner Join_Where Clause - Fatal编程技术网

基于最大时间戳的SQL内部联接

基于最大时间戳的SQL内部联接,sql,sqlite,max,inner-join,where-clause,Sql,Sqlite,Max,Inner Join,Where Clause,修订一次 修改两次:除报告外,其余9个表的标题始终称为what 我有大约10张表,结构如下: reports (165k rows) +-----------+-----------+ | identifier| category | +-----------+-----------+ | 1 | fixed | | 2 | wontfix | | 3 | fixed | | 4 | invalid |

修订一次

修改两次:除报告外,其余9个表的标题始终称为what

我有大约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       |
+-----------+-----------+--------------+------------+
也就是说,query=reportsfinal之后的报告作为基础表,我必须从其他9个表中添加一列或两列。标识符是键,但在某些表中,标识符会出现多次。在这些情况下,我只想使用时间最长的条目。 我试过几个问题,但都不管用。如果可能的话,我想用这种方法运行一个查询,从其他9个表中获取不同的列

我的尝试基于以下答案:

选择T.identifier, T.类别, t、 什么是什么, t、 你从哪来的 选择R.identifier, R.类别, 结合,什么,不,什么, 联合,什么,不,什么, 行号按R.标识符划分,R.类别顺序按选择空 来自报告R 左连接错误 在S.identifier=R.identifier上 左连接优先级P 在P.identifier=s.identifier上 按R.标识符、R.类别、S.what、P.what分组 其中T.RN=1
按T.identifier排序 对于每个关联的表,只需使用基于子查询的谓词来标识特定的时间戳

单字母标记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);
问题:为什么要将列从状态和优先级重命名为什么??你不妨说出一些东西、数据或信息。至少原名状态和prio传达了一些信息。。这个词毫无意义


注意。我取消了对what11和what12别名的编辑,因为这些名称没有意义。

基本上,您需要在选择列表中找到相关的子查询

从臀部,比如:

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

使用Row_number的工作基于您假定的数据

select  T.identifier,
        T.category,
        what AS what11,
        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 status 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

@彼得:a代表什么?a=报告吗?此外,Top1在SQLite中不起作用,我读到Limit1起作用。像这样,我没有得到错误,但查询仍在运行,我有300k行:“选择reports.identifier,reports.current\u resolution,reports.current\u status,从bugstatus中选择what,其中bugstatus.identifier=reports.identifier按时间戳排序desc LIMIT 1 what,选择来自优先级的内容,其中priority.identifier=reports.identifier按时间戳排序desc LIMIT 1来自报告的内容`是的,我说这是时髦的,我用的是T-SQL,我想你用的是Sqllite。。因此,极限1似乎是等效的。是的,我本想把报告化名为A,但没有。你是说它有效还是无效?是的,我正在使用SQLite。那么,我是否需要在查询结束时从报告a中获取信息?我的提案的其余部分加上新标题是否正确?非常感谢。@Pilotbob是的,这应该会使报告成为。。。或者您可以在查询的其余部分将其从a更改为reports。我认为您的编辑看起来是正确的。我编辑它是为了添加别名并使用sqlite limit 1语法。我收到“错误:不明确的列名:时间”我看到了您关于标题的观点。我没有这样定义它们,它们在csv文件中是这样定义的。但是你是对的,我本来可以在创建SQLite数据库时给它们起更多有意义的名字的。查询现在已经运行了4个小时,但仍然没有完成。“我想一定有一个查询可以更快地获取数据?”查尔斯·布雷塔纳多(Charles BretanaDo)外键有标记?对不起,我不能回答这个问题。这些表格如我的问题所述。在reports表中,主键是identifier。在其他表中,唯一标识符是标识符和时间的组合。@pilotbobi如果您可以联系dbo的任何人,请询问他们是否可以添加status.time和priority.time的索引。另外,如果是SQL Server,或者无论它是什么数据库,都使用聚集索引,我会将这两个表上的主键聚集索引放在两个字段标识符、时间的组合上。这是SQL Server,还是Oracle或MySQL??因为什么不代表什么,或者它的定义在哪里?ORDER by select nullRN表示什么或在何处定义?非常感谢!我用它作为派生表,T只是别名。RN是行号,为便于订购,请选择Null@JohnDavison1。别名和表名可以互换使用小写和大写字母。那有关系吗?2表中没有明确的NAs,只有空白。代码必须更改吗?3我将标题更改为后续表格的状态和优先级。如果所有标题都被称为什么,这是一个问题吗?我得到错误:error:near:syntax error。@mohan111NA只不过是NULL。您可以更改为NULL,然后转到syntax error。正确给出列名。编辑的代码是否正确?关于初始问题中的新标题?别名是r还是r有关系吗@磨憨111