繁体   English   中英

如何计算所有行2列中的不同值

[英]How to count distinct values over all rows 2 columns

我有2列,并且我希望在整个BOTH列中查找所有不同值的计数,而不仅仅是两列中的同一行。 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

您可以取消数据透视和计数:

select count(distinct x)
from ((select x from t) union all
      (select y from t)
     ) t;

就是说,看起来第一列是唯一的,并且具有您想要的信息,所以也许您只是想要:

select count(*)
from t;

要么:

select count(distinct x)
from t;

联盟是要走的路

    Select count(*) from(Select x from 
    table tx 
   union
   Select y from table ty) ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM