SQL,在多列中计数然后分组 [英] SQL, count in multiple columns then group by

查看:25
本文介绍了SQL,在多列中计数然后分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在多列中计数,然后按总和分组,其中任何列中出现相同的数据

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   b   
a   a   a
b   c   
a   b   b
b   a

我希望它显示如下内容:

I want it to show something like this:

Desired query output:

     Total
   -------------
a |    6
b |    5
c |    1

推荐答案

可以使用联合查询

SELECT x.f1,Count(x.f1) FROM
(SELECT p1 As F1 FROM table
 UNION ALL
 SELECT p2 As F1 FROM table
 UNION ALL
 SELECT p3 As F1 FROM table) x
GROUP BY x.f1

这篇关于SQL,在多列中计数然后分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆