简体   繁体   English

在clickhouse中与max()聚合时如何选择一行中的相应值?

[英]How to select corresponding value in a row when aggregating with max() in clickhouse?

I have a table in db like this:我在数据库中有一个这样的表:

This is a part for one UserID, but in fact there are many of them.这是一个 UserID 的一部分,但实际上有很多。

create table MY_TABLE
(
    UserID Nullable(String),
    OID int,
    TotalHits Nullable(int),
    DaysOfHits Nullable(int),
    UniqPrimaryEvents Nullable(int)
)
engine = Memory;

insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6564023, 4, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6546504, 9, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6538286, 12, 2, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6536273, 8, 2, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6534195, 57, 6, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6528643, 4, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6496311, 7, 2, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6492524, 7, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6475804, 9, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6424164, 5, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6403817, 8, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6403592, 9, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6400394, 13, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6383627, 8, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6364163, 4, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6349018, 7, 1, 0);
insert into MY_TABLE (UserID, OID, TotalHits, DaysOfHits, UniqPrimaryEvents) values ('1000c666-04db-4447-9ea1-ecf1e2275c81', 6270551, 6, 1, 0);

I need to aggregate the table, and have in results both results of agg funcs and corresponding value from OID:我需要聚合表,并在结果中包含 agg funcs 的结果和来自 OID 的相应值:

I'm doing smth like:我正在做这样的事情:

SELECT
    UserID,
    uniq(OID) AS UniqObjects,
    sum(TotalHits) AS TotalHits,
    round(avg(DaysOfHits), 2) AS AvgObjectHitDays,
    max(DaysOfHits) AS MaxHitPeriod,
-- here I need OID corresponding to max(DaysOfHits) value
    round((avg(DaysOfHits) / max(DaysOfHits)) * 100, 2) AS PerOfMaxHit

I tried smth like anyIf(OID, DaysOfHits = max(DaysOfHits)), but you can't have agg func inside agg func.我尝试过类似anyIf(OID, DaysOfHits = max(DaysOfHits)),但是你不能在 agg func 中使用 agg func。

PS the source of Select is another joined select, not a single table. PS Select 的来源是另一个加入的选择,而不是单个表。

Please, help!请帮忙!

argMax is a great thing! argMax 是个好东西! And I'm just a beginner, sorry)我只是一个初学者,对不起)

the answer is argMax(OID, DaysOfHits) - it will return OID for max of DaysOfHits, as was required答案是 argMax(OID, DaysOfHits) - 它将返回最大 DaysOfHits 的 OID,按照要求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM