Skip to content

Commit

Permalink
A much shorter query!
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Shanley <[email protected]>
  • Loading branch information
ErinLMoore and arkadyan committed Jul 19, 2023
1 parent 2dd7022 commit 5d35479
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions apps/alert_processor/lib/model/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -250,27 +250,15 @@ defmodule AlertProcessor.Model.Query do
%__MODULE__{
label: "Count of Active Users By Number of Active Trips",
query: """
SELECT integers.*
FROM generate_series(1, (
SELECT COUNT(ttr.id) as trip_count from trips ttr
group by ttr.user_id
ORDER BY trip_count DESC
LIMIT 1
)) i
CROSS JOIN LATERAL (
SELECT i AS active_trips, COUNT(x.*) AS active_users FROM
(
SELECT u.id FROM users u
JOIN trips tr ON tr.user_id = u.id
WHERE tr.id = ANY(
SELECT DISTINCT ss.trip_id FROM subscriptions ss
WHERE ss.paused = false
)
GROUP BY u.id
HAVING COUNT(tr.id) = i
)x
HAVING COUNT(x) > 0
) integers;
select trip_count, count(user_id) as users
from (
select t.user_id as user_id, count(distinct t.id) as trip_count
from trips t
join subscriptions s
on s.trip_id = t.id and s.paused = false
group by t.user_id
) as user_trip_counts
group by trip_count;
"""
}
]
Expand Down

0 comments on commit 5d35479

Please sign in to comment.