SQL aggregation using group by of columns one by one

Singh :

Scenario:

Below is my table.

+-------+-----------+--------+--------+
|col2   |col3       |col4    |col5    |
+-------+-----------+--------+--------+
|    1.0|          2|       a|      a1|
|    1.0|          1|       a|      a2|
|    1.0|          2|       b|      a3|
|    2.0|          1|       a|      a1|
|    2.0|          1|       a|      a2|
+-------+-----------+--------+--------+

I need to get the aggregated result as below.

+-------+-----------+-----------+-----------+-----+
|col2   |col3       | field_name|field_value|count|
+-------+-----------+-----------+-----------+-----+
|    2.0|          1|   col3    |          1|    2|
|    1.0|          1|   col3    |          1|    1|
|    1.0|          2|   col3    |          2|    2|
|    2.0|          1|   col4    |          a|    2|
|    1.0|          1|   col4    |          a|    1|
|    1.0|          2|   col4    |          a|    1|
|    1.0|          2|   col4    |          b|    1|
|    1.0|          1|   col5    |         a2|    1|
|    1.0|          2|   col5    |         a1|    1|
|    1.0|          2|   col5    |         a3|    1|
|    2.0|          1|   col5    |         a1|    1|
|    2.0|          1|   col5    |         a2|    1|
+-------+-----------+-----------+-----------+-----+

Implemented Solution:

I implemented the solution by creating three different tables[T1, T2, T3]. And then for each table, I created [rowset1, rowset2, rowset3] programmatically to combine all into a single table

select col2, col3, col3, count(*) from calc group by col2, col3;
T1 :
|col2   |col3       |col3       |count|
+-------+-----------+-----------+-----+
|    2.0|          1|          1|    2|
|    1.0|          1|          1|    1|
|    1.0|          2|          2|    2|
+-------+-----------+-----------+-----+

rowset1 = [[2.0,1,col3,1,2,18000], [1.0,1,col3,1,1,18000], [1.0,2,col3,2,2,18000]]

select  col2, col3,  col4, count(*) from calc group by col2, col3, col4;
T2:
+-------+-----------+--------+-----+
|col2   |col3       |col4    |count|
+-------+-----------+--------+-----+
|    2.0|          1|       a|    2|
|    1.0|          1|       a|    1|
|    1.0|          2|       a|    1|
|    1.0|          2|       b|    1|
+-------+-----------+--------+-----+
rowset2 = [[2.0,1,col4,a,2,18000], [1.0,1,col4,a,1,18000], [1.0,2,col4,a,1,18000],[1.0,2,col4,b,1,18000]]

select col2, col3, col5 , count(*) from calc group by col2, col3, col5;
T3:
+-------+-----------+--------+-----+
|col2   |col3       |col5    |count|
+-------+-----------+--------+-----+
|    1.0|          1|      a2|    1|
|    1.0|          2|      a1|    1|
|    1.0|          2|      a3|    1|
|    2.0|          1|      a1|    1|
|    2.0|          1|      a2|    1|
+-------+-----------+--------+-----+

rowset3 = [1.0,2,col5,b,1,18000], [1.0,1,col5,a2,1,18000], [1.0,2,col5,a1,1,18000], [1.0,2,col5,a3,1,18000], [2.0,1,col5,a1,1,18000], [2.0,1,col5,a2,1,18000]]


Problem:

How can I achieve the same in SQL without an overhead of creating rowset1,2,3 and combining it into a single table?

Gordon Linoff :

If I understand correctly, you want to do three aggregations in one. One method is to use cross join to bring in the information to distinguish each aggregation:

select col2, col3, field_name,
       (case when field_name = 'col3' then col3
             when field_name = 'col4' then col4
             when field_name = 'col5' then col5
        end) as field_value,
       count(*) as cnt
from t cross join
     (select 'col3' as field_name union all
      select 'col4' as field_name union all
      select 'col5' as field_name
     ) f
group by col2, col3, field_name, field_value
order by field_name, col2 desc, col3;

Here is a db<>fiddle.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=5609&siteId=1
one
ONE