Slackbot
01/17/2023, 6:15 PMJohn Kowtko
01/17/2023, 6:30 PMJoshua Liguid
01/17/2023, 6:50 PMJohn Kowtko
01/17/2023, 6:55 PMJohn Kowtko
01/17/2023, 7:12 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,
count(b.column1) "rank"
from tmp a
LEFT 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
The inequality had to be left out of the join condition because druid doesn't support inequality join conditions ... for that reason you have to use >= instead of just > when determining rank count, otherwise the top rank record (with no B match) will get knocked out of the query.
Let me know if you have any questions on this.Vadim
01/17/2023, 7:15 PMRANK is coming to town:Vadim
01/17/2023, 7:17 PMSergio Ferragut
01/17/2023, 8:22 PMSELECT * FROM
(
SELECT "countryName",
"page",
SUM ("added") count1,
SUM ("deleted") count2
FROM
"wikipedia_r" aa
GROUP BY
1,
2
ORDER BY 3 DESC
LIMIT 10
)
UNION ALL
(
SELECT "countryName",
"page",
SUM ("added") count1,
SUM ("deleted") count2
FROM
"wikipedia_r" aa
GROUP BY
1,
2
ORDER BY 4 DESC
LIMIT 10
)
The result is the list of 20 items that are either in the top 10 based on count1 or top 10 based on count2Sergio Ferragut
01/17/2023, 8:25 PMJoshua Liguid
01/17/2023, 8:49 PMJoshua Liguid
01/17/2023, 8:49 PMJohn Kowtko
01/18/2023, 3:21 AMJoshua Liguid
01/18/2023, 1:32 PMJohn Kowtko
01/18/2023, 3:20 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
This works for me and displays ties at the higher rank (e.g.1, 1, 3, 4, 5) instead of the lower rank (2, 2, 3, 4, 5)
Yes, it will be a lot easier, and more readable, when the windowing functions are released 🙂Joshua Liguid
01/18/2023, 5:32 PMSergio Ferragut
01/18/2023, 7:37 PMwith tmp1 as
(
SELECT "countryName",
"page",
SUM ("added") count1,
SUM ("deleted") count2
FROM
"wikipedia_r" aa
GROUP BY
1,
2
ORDER BY 3 DESC
LIMIT 10
),
tmp2 AS
(
SELECT "countryName",
"page",
SUM ("added") count1,
SUM ("deleted") count2
FROM
"wikipedia_r" aa
GROUP BY
1,
2
ORDER BY 4 DESC
LIMIT 10
)
select a."countryName", a."page", a.count1, a.count2
,sum(case when b.count1 > a.count1 then 1 else 0 end)+1 "rank"
from tmp1 a
, tmp1 b
group by 1,2,3,4
UNION ALL
select a."countryName", a."page", a.count1, a.count2
,sum(case when b.count2 > a.count2 then 1 else 0 end)+1 "rank"
from tmp2 a
, tmp2 b
group by 1,2,3,4
This will always keep the product join in the self-joins limited to 10x10 =100 rows, so it should be fast on the broker. It will not include more than 10 rows, in each ranking, so if there are ties, you will not get to rank=10.
Rank function will be really nice to have. 😄