1

I have a few different queries for getting some counters. Is it possible to combine these queries to one, but not to sum&union the counters?

For example:

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

and

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

and

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

And what I expect:

| ta | tap | tau | tm | tmp | tmu | tp  | tpw | tpm | tpy |
|----|-----|-----|----|-----|-----|-----|-----|-----|-----|
| 20 | 10  | 10  | 12 | 10  | 2   | 230 | 30  | 180 | 220 |

1 Answer 1

2

You could just CROSS JOIN the three queries:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.