KamoCRM

Org directory SQL with per-facet CTEs

FeatureBillingService
Shipped
8 ஆகஸ்ட், 2026 அன்று 5:04 PM UTC
Author
Kamo
Commit
10c3f3c

Held accounts, inbound subscriptions, members, licensees and children each aggregate in their own CTE before being joined to orgs. Joining them in one pass would multiply rows and inflate every SUM. Pure and unit-tested (26 tests): no '::' casts, every value bound, sort keys whitelisted through OrgSort. Two things the tests pin because they are easy to "tidy" into bugs -- ORDER BY and the derived predicates (mode_rank, has_stripe) must name the base CTE's projected columns, since the `o` alias is out of scope in the outer query; and a PENDING trial reports no deadline rather than the latest expired one. Verified against the live cluster: both statements execute, the count agrees with the rows, and filters/aggregates apply. Index usage is NOT demonstrated -- at 12 orgs / 9 subs CockroachDB full-scans regardless, correctly.

All changes

Like what you see shipping?

All of it arrives in your workspace on its own. Start on the free plan and read this page again in a month.

Start Free ForeverView Pricing