Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions sql/reports/topgear/hourly.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
WITH bill AS (
SELECT
cb."challengeId" AS challenge_id,
MAX(cb."billingAccountId") FILTER (WHERE cb."billingAccountId" IS NOT NULL) AS billing_account_id
FROM challenges."ChallengeBilling" cb
WHERE cb."billingAccountId" = '80000062'
GROUP BY cb."challengeId"
),
base_challenges AS (
WITH base_challenges AS (
SELECT
c.*,
b.billing_account_id
ba.billing_account_id
FROM challenges."Challenge" c
JOIN bill b
ON b.challenge_id = c.id
JOIN LATERAL (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ performance]
The use of JOIN LATERAL with ON TRUE is correct here, but ensure that the performance impact is acceptable. LATERAL joins can be more expensive than regular joins, especially if the subquery is complex or returns a large number of rows. Consider testing the query performance if this section is a bottleneck.

SELECT
MAX(cb."billingAccountId") AS billing_account_id
FROM challenges."ChallengeBilling" cb
WHERE cb."challengeId" = c.id
AND cb."billingAccountId" = '80000062'
) ba ON TRUE
WHERE c."createdAt" >= now() - interval '4 months'
AND ba.billing_account_id IS NOT NULL

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[💡 maintainability]
The condition AND ba.billing_account_id IS NOT NULL is redundant since the subquery already filters for billingAccountId = '80000062'. This condition can be removed to simplify the query.

),
project_details AS (
SELECT
Expand Down