Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql countDistinct和distinct.count之间的差异_Sql_Scala_Apache Spark_Apache Spark Sql - Fatal编程技术网

Sql countDistinct和distinct.count之间的差异

Sql countDistinct和distinct.count之间的差异,sql,scala,apache-spark,apache-spark-sql,Sql,Scala,Apache Spark,Apache Spark Sql,为什么将.agg(countDistinct(“member_id”)作为“count”)和.distinct.count得到不同的输出? 区别是否与选择计数(不同成员id)和选择不同计数(成员id)之间的区别相同?第一个命令: DF.agg(countDistinct("member_id") as "count") DF.distinct.count 返回与从DF中选择计数不同(成员id)相同的值 第二个命令: DF.agg(countDistinct("member_id") as "

为什么将
.agg(countDistinct(“member_id”)作为“count”)
.distinct.count
得到不同的输出?
区别是否与
选择计数(不同成员id)
选择不同计数(成员id)
之间的区别相同?

第一个命令:

DF.agg(countDistinct("member_id") as "count")
DF.distinct.count
返回与从DF中选择计数不同(成员id)相同的值

第二个命令:

DF.agg(countDistinct("member_id") as "count")
DF.distinct.count
实际上是获取不同的记录或从DF中删除所有重复项,然后进行计数

为什么..agg(countDistinct(“member_id”)作为“count”)和..distinct.count会得到不同的输出

因为
.distinct.count
是相同的:

SELECT COUNT(*) FROM (SELECT DISTINCT member_id FROM table)
.agg(countDistinct(“成员id”)作为“count”)

SELECT COUNT(DISTINCT member_id) FROM table
计数(*)

返回
成员id
列的不同值的数目,忽略所有其他列,同时

df.distinct.count
将统计数据帧中不同的记录的数量,其中“不同”表示所有列的值相同

例如,数据帧:

+-----------+---------+
|member_name|member_id|
+-----------+---------+
|          a|        1|
|          b|        1|
|          b|        1|
+-----------+---------+

只有一个不同的
成员id
值,但有两个不同的记录,因此
agg
选项将返回1,而后者将返回2。

因此,从技术上讲,输出应该是相同的?否。第一个特定于成员id列。第二个在所有列上。更具体地说,第二个在所有列上都有group by,其中第一个在1列上有group by。那么请在问题中说明。。。如果您编辑文章以添加一个输入示例,您将得到两个不同的结果,这将非常有用。或者至少是数据帧的模式。