about summary refs log tree commit diff
path: root/db/views/instances_v01.sql
blob: 94acd61a133266aaa70e4020e594872c5bb73960 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WITH domain_counts(domain, accounts_count)
AS (
  SELECT domain, COUNT(*) as accounts_count
  FROM accounts
  WHERE domain IS NOT NULL
  GROUP BY domain
)
SELECT domain, accounts_count
FROM domain_counts
UNION
SELECT domain_blocks.domain, COALESCE(domain_counts.accounts_count, 0)
FROM domain_blocks
LEFT OUTER JOIN domain_counts ON domain_counts.domain = domain_blocks.domain
UNION
SELECT domain_allows.domain, COALESCE(domain_counts.accounts_count, 0)
FROM domain_allows
LEFT OUTER JOIN domain_counts ON domain_counts.domain = domain_allows.domain