我有一些不同的查询来获取一些计数器。是否可以将这些查询合并为一个查询,但不能对计数器求和与求和?
例如:
SELECT
COUNT(uuid) AS total_admins ta,
SUM(CASE WHEN isPublished = 1 THEN 1 ELSE 0 END) AS total_admins_published tap,
SUM(CASE WHEN isPublished = 0 THEN 1 ELSE 0 END) AS total_admins_unpublished tau
FROM admins
和
SELECT
COUNT(uuid) AS total_mediators tm,
SUM(CASE WHEN isPublished = 1 THEN 1 ELSE 0 END) AS total_mediators_published tmp,
SUM(CASE WHEN isPublished = 0 THEN 1 ELSE 0 END) AS total_mediators_unpublished tmu
FROM mediator
和
SELECT
COUNT(uuid) AS total_posts tp,
SUM(CASE WHEN submissionDate BETWEEN "start" AND "end" THEN 1 ELSE 0 END) AS total_posts_week tpw,
SUM(CASE WHEN submissionDate BETWEEN "start" AND "end" THEN 1 ELSE 0 END) AS total_posts_month tpm,
SUM(CASE WHEN submissionDate BETWEEN "start" AND "end" THEN 1 ELSE 0 END) AS total_posts_year tpy
FROM posts
以及我的期望:
| ta | tap | tau | tm | tmp | tmu | tp | tpw | tpm | tpy |
|----|-----|-----|----|-----|-----|-----|-----|-----|-----|
| 20 | 10 | 10 | 12 | 10 | 2 | 230 | 30 | 180 | 220 |
您可以只CROSS JOIN
这三个查询:
SELECT ta, tap, tau, tm, tmp, tmu, tp, tpw, tpm, tpy
FROM (
SELECT
COUNT(uuid) AS total_admins ta,
SUM(CASE WHEN isPublished = 1 THEN 1 ELSE 0 END) AS total_admins_published tap,
SUM(CASE WHEN isPublished = 0 THEN 1 ELSE 0 END) AS total_admins_unpublished tau
FROM admins
) a
CROSS JOIN (
SELECT
COUNT(uuid) AS total_mediators tm,
SUM(CASE WHEN isPublished = 1 THEN 1 ELSE 0 END) AS total_mediators_published tmp,
SUM(CASE WHEN isPublished = 0 THEN 1 ELSE 0 END) AS total_mediators_unpublished tmu
FROM mediator
) m
CROSS JOIN (
SELECT
COUNT(uuid) AS total_posts tp,
SUM(CASE WHEN submissionDate BETWEEN "start" AND "end" THEN 1 ELSE 0 END) AS total_posts_week tpw,
SUM(CASE WHEN submissionDate BETWEEN "start" AND "end" THEN 1 ELSE 0 END) AS total_posts_month tpm,
SUM(CASE WHEN submissionDate BETWEEN "start" AND "end" THEN 1 ELSE 0 END) AS total_posts_year tpy
FROM posts
) p