SQL, count in multiple columns then group by

后端 未结 2 1791
礼貌的吻别
礼貌的吻别 2021-01-12 16:52

I am trying to count in multiple columns then group by for a total sum where the same data appears in any column

Source data table:

P1  P2  P3
-----------
a         


        
2条回答
  •  孤街浪徒
    2021-01-12 17:42

    You can union all the records in a subquery and on the outer query, count each value.

    SELECT b.a, COUNT(b.a)
    FROM
        (
            SELECT P1 a
            FROM tableName
            UNION ALL
            SELECT P2 a
            FROM tableName
            UNION ALL
            SELECT P3 a
            FROM tableName
        ) b
    GROUP BY b.a
    

提交回复
热议问题