【问题标题】:Find min max with other attribute?用其他属性找到最小值最大值?
【发布时间】:2020-10-20 15:38:46
【问题描述】:

假设我有一张桌子 time_created, view_type, view_idclickhouse 中的列。

如何使用按view_type 分组的适当view_id 找到最小和最大time_created

这是启动sql:

select min(time_created) as min_time_created,
max(time_created) as max_time_created,
view_type  from views 
group by view_type;

以及如何将view_id 添加到每个结果列。还要考虑time_created 不是唯一的。

【问题讨论】:

  • “同时考虑 time_created 不是唯一的”——这是否意味着一个最小/最大 time_created 可能与多个 view_id 相关?您能否描述输入数据和所需结果示例的极端情况?
  • @vladimir,是的,一次可能有很多视图。猜猜不需要极端案例,因为您的答案正是我想要的。谢谢!

标签: sql clickhouse


【解决方案1】:

看起来这个查询应该有帮助:

SELECT view_type, 
  min(time_created) as min_time_created,
  max(time_created) as max_time_created
  argMin(view_id, time_created) as min_view_id, 
  argMax(view_id, time_created) as max_view_id
FROM views
GROUP BY view_type

【讨论】:

    【解决方案2】:

    试试这个并使用窗口功能

    Select 
          MIN(time_created) OVER (PARTITION BY view_type) AS Min_time_created,
          MAX(time_created) OVER (PARTITION BY view_type) AS Min_time_created,
          View_type  
    from Views ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-06
      • 2016-10-13
      • 2012-06-21
      • 1970-01-01
      • 2017-05-30
      • 2012-05-05
      • 2015-09-25
      • 1970-01-01
      相关资源
      最近更新 更多