Slackbot
08/16/2023, 2:23 PMJohn Kowtko
08/16/2023, 3:31 PMwindowsAreForClosers:true
Please note that this implementation requires that the query contains a Group By ... which it looks like yours does.
Let us know if you have any issues with this.Ben Krug
08/16/2023, 4:58 PMsriramdas sivasai
08/16/2023, 5:24 PMHellmar Becker
08/16/2023, 5:33 PMsriramdas sivasai
08/16/2023, 5:50 PMJohn Kowtko
08/16/2023, 6:19 PMwith tmp as (
select channel column1, flags column2, sum(added) count1, sum(deleted) count2
from "wikipedia"
group by 1, 2
)
select a.column1, a.column2, a.count1, a.count2,
sum(case when b.count1 > a.count1 then 1 else 0 end)+1 "rank"
from tmp a
JOIN tmp b ON b.column1 = a.column1
group by a.column1, a.column2, a.count1, a.count2
order by 1, 5
there is a variation of this when you add the inequality filter in a WHERE clause, the difference being in how you treat ties:
with tmp as (
select channel column1, flags column2, sum(added) count1, sum(deleted) count2
from "wikipedia"
group by 1, 2
)
select a.column1, a.column2, a.count1, a.count2,
count(b.column1) "rank"
from tmp a
JOIN tmp b ON b.column1 = a.column1
where b.count1 >= a.count1
group by a.column1, a.column2, a.count1, a.count2
order by 1, 5
You might use > instead of >=, and/or count(*)+1 instead of just count(*) , depending on how you want the rank numbers to end up.
Go ahead and try this, if you can't get to the exact result that you are looking for, please post what you have and we can go from there.
Thanks. JohnCharles Smith
08/16/2023, 6:44 PMsriramdas sivasai
08/17/2023, 3:49 PMsriramdas sivasai
08/17/2023, 3:50 PMMaytas Monsereenusorn
08/26/2023, 12:05 AMMaytas Monsereenusorn
08/26/2023, 12:07 AMBen Krug
08/26/2023, 12:12 AMMaytas Monsereenusorn
08/26/2023, 12:12 AMMaytas Monsereenusorn
08/26/2023, 12:12 AMBen Krug
08/26/2023, 12:15 AMBen Krug
08/26/2023, 12:17 AMMaytas Monsereenusorn
08/26/2023, 12:52 AMBen Krug
08/26/2023, 1:21 AMJohn Kowtko
08/26/2023, 4:54 AMinsert into wiki_cumulative select __time, channel, page, added from wikipedia partitioned by ALL
Then you can run the self-join query as a REPLACE/SELECT to add the cumulative field:
replace into wiki_cumulative overwrite all
select a.channel, a.__time, a.page, a.added, sum(b.added) cumAdded
from wiki_cumulative a join wiki_cumulative b on a.channel = b.channel
where b.__time <= a.__time
group by 1, 2, 3, 4
partitioned by all
... and select out the results:
select channel, __time, page, added, cumAdded from wiki_cumulative group by 1, 2, 3, 4, 5 order by 1, 2
Results should look like in the screenshot below. (Fyi the group by in this select statement is added to allow ordering by channel first.)Maytas Monsereenusorn
08/26/2023, 5:06 AMJohn Kowtko
08/26/2023, 1:37 PMselect channel, __time, page, added,
sum(added) over (partition by channel order by __time rows unbounded preceding) cumAdded
from "wikipedia"
group by 1, 2, 3, 4
(... and corresponding results screenshot ...)Maytas Monsereenusorn
08/26/2023, 9:04 PMJohn Kowtko
08/26/2023, 10:36 PMsqlJoinAlgorithm:broadcast) ... but if the cardinality is high here and up hit up against broadcast limits on the self-join you may need to switch to sortMerge
ref: https://druid.apache.org/docs/latest/multi-stage-query/reference/#joins