[英]How to count distinct values over all rows 2 columns
I have 2 columns,and I am looking for a count of all distinct values in BOTH columns overall, not just the same row in both columns. 我有2列,并且我希望在整个BOTH列中查找所有不同值的计数,而不仅仅是两列中的同一行。 IE here the count of distinct values is 9 because 1000, 5000, 7000 and 8000 will only be included once. IE此处的不同值计数为9,因为1000、5000、7000和8000仅包含一次。
x y
1000 NULL
2000 1000
3000 1000
4000 1000
5000 1000
6000 5000
7000 5000
8000 7000
9000 8000
You can unpivot the data and count: 您可以取消数据透视和计数:
select count(distinct x)
from ((select x from t) union all
(select y from t)
) t;
That said, it looks like the first column is unique and has the information you want, so perhaps you simply want: 就是说,看起来第一列是唯一的,并且具有您想要的信息,所以也许您只是想要:
select count(*)
from t;
Or: 要么:
select count(distinct x)
from t;
Union is the way to go 联盟是要走的路
Select count(*) from(Select x from
table tx
union
Select y from table ty) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.