【发布时间】:2021-09-30 12:49:36
【问题描述】:
我在数据库中有一个这样的表:
这是一个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);
我需要聚合表格,并在结果中包含 agg funcs 的结果和来自 OID 的相应值:
我正在做类似的事情:
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
我试过像anyIf(OID, DaysOfHits = max(DaysOfHits)), 这样的东西,但你不能在 agg func 里面有 agg func。
PS Select 的来源是另一个joined select,而不是单个表。
请帮忙!
【问题讨论】:
-
你能告诉我们你希望你的输出在 desc 中的样子吗?
标签: sql database clickhouse