SQL-计算不同列上的重复发生

问题描述

我正在尝试查找不同列上所有值的重复发生总数。

例如:

example

有什么建议吗?谢谢

解决方法

您可以先取消透视,然后进行汇总。如果您的数据库支持values()和横向联接:

select x.name,count(*) cnt
from mytable
cross join lateral (values (name1),(name2),(name3)) as x(name)
group by x.name

union all是一种更具移植性(尽管效率较低)的方法:

select name,count(*) cnt
from (
    select name1 as name from mytable
    union all select name2 from mytable
    union all select name3 from mytable
) x
group by name

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...