Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.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/0/unity3d/4.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 从oracle中为每个组选择最新的行_Sql_Oracle_Greatest N Per Group - Fatal编程技术网

Sql 从oracle中为每个组选择最新的行

Sql 从oracle中为每个组选择最新的行,sql,oracle,greatest-n-per-group,Sql,Oracle,Greatest N Per Group,我在留言簿中有一个包含用户评论的表格。列包括:id、用户id、标题、注释、时间戳 我需要为每个用户选择最新的行。 我已尝试使用group by执行此操作,但尚未对其进行管理,因为我无法在同一查询中选择任何其他内容,其中我按用户id分组: SELECT user_id, MAX(ts) FROM comments GROUP BY user_id 例如,在这个查询中,我不能添加到select列id、tilt和comment。如何做到这一点?您可以使用连接来构建查询: select c.* fro

我在留言簿中有一个包含用户评论的表格。列包括:id、用户id、标题、注释、时间戳

我需要为每个用户选择最新的行。 我已尝试使用group by执行此操作,但尚未对其进行管理,因为我无法在同一查询中选择任何其他内容,其中我按用户id分组:

SELECT user_id, MAX(ts) FROM comments GROUP BY user_id

例如,在这个查询中,我不能添加到select列id、tilt和comment。如何做到这一点?

您可以使用
连接来构建查询:

select c.*
from comments c join
     (select user_id, max(ts) as maxts
      from comments c2
      group by user_id
     ) cc
     on c.user_id = cc.user_id and c.ts = cc.maxts;
还有其他方法。典型的建议是使用
行编号()


这两个查询略有不同。如果用户的最新注释具有完全相同的
ts
,则第一个注释将返回重复的注释。第二个函数为每个用户返回一行。

您可以使用分析函数

SELECT *
  FROM (SELECT c.*,
               rank() over (partition by user_id order by ts desc) rnk
          FROM comments c)
 WHERE rnk = 1

根据您希望如何处理关系(如果可能有两行具有相同的
用户id
ts
),您可能希望使用
行编号
密集列
功能,而不是
rank
将允许在出现平局时多行在第一行<代码>行号
将在出现平局时任意返回一行<代码> DeNeSyReals对于第一个并列的行,其行为类似于“代码>秩< /代码>,但假定第一行为第二行而不是第三行,假设为两行并列。

< P>这类问题具有非常简单且非常有效的解决方案,<<代码>密集排名第一/最后函数:

select id,
       max(user_id) keep (dense_rank last order by ts) over (partition by id) as user_id,
       max(title)   keep (dense_rank last order by ts) over (partition by id) as title,
       max(comment) keep (dense_rank last order by ts) over (partition by id) as comment,
       max(ts)                                                                as ts
from   comments;
select id,
       max(user_id) keep (dense_rank last order by ts) over (partition by id) as user_id,
       max(title)   keep (dense_rank last order by ts) over (partition by id) as title,
       max(comment) keep (dense_rank last order by ts) over (partition by id) as comment,
       max(ts)                                                                as ts
from   comments;