1

Suppose there is a table:

first second third
1 1 1
1 1 1
1 2 2
1 1 2
2 3 1

And by query:

SELECT first, count(*) AS cnt
FROM table
GROUP BY first

We get result:

first cnt
1 4
2 1

How to calculate count of another column (columns) inside group? Expected result:

first cnt aggregated_count_by_second aggregated_count_by_third
1 4 {1: 3, 2: 1} {1: 2, 2: 2}
2 1 {3: 1} {1: 1}

DB version: 10.3.39-MariaDB-0+deb10u1

2
  • 3
    The task can be easily solved on MariaDB 10.5+: dbfiddle.uk/deWSlyvq Maybe it's a time to upgrade?
    – Akina
    Nov 6 at 16:55
  • "Easily solved"?? The point is that you cannot do it in a single grouping; instead you need to do separate steps, then put the pieces together.
    – Rick James
    Nov 10 at 18:11

0

Your Answer

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

Browse other questions tagged or ask your own question.