用其他属性找到最小值最大值?
Posted
技术标签:
【中文标题】用其他属性找到最小值最大值?【英文标题】:Find min max with other attribute? 【发布时间】:2020-10-20 15:38:46 【问题描述】:假设我有一张桌子
time_created, view_type, view_id
clickhouse 中的列。
如何使用按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,是的,一次可能有很多视图。猜猜不需要极端案例,因为您的答案正是我想要的。谢谢! 【参考方案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 ;
【讨论】:
以上是关于用其他属性找到最小值最大值?的主要内容,如果未能解决你的问题,请参考以下文章