简体   繁体   English

如何计算 SQL 中每个分区的不同对数?

[英]How to count number of distinct pairs per partition in SQL?

I want to get the # of distinct pair combinations per level in my table.我想在我的表中获得每个level的不同对组合的数量。

Sample Data:样本数据:

level       group_no    item_no
oiegown     1           1
oiegown     1           2
oiegown     1           2
oiegown     1           3
oiegown     2           1
wefwefw     1           1
wefwefw     2           2

My Attempt:我的尝试:

Wanted to do something like想做类似的事情

COUNT(DISTINCT group_no, item_no) 
  OVER (PARTITION BY level)
AS item_count

Expected Output:预期 Output:

level       item_count
oiegown     4           
wefwefw     2           

But seems that COUNT() only accepts one argument in BigQuery.但似乎COUNT()在 BigQuery 中只接受一个参数。 How can I modify my query to get my desired result?如何修改我的查询以获得我想要的结果?

You can join both column into one Text, and count the concatenated text您可以将两列合并为一个文本,并计算连接的文本

COUNT(DISTINCT CONCAT(group_no, item_no)) 
  OVER (PARTITION BY level)
AS item_count

Use below query for the expected out:使用以下查询获取预期结果:

SELECT level, COUNT(DISTINCT group_no || item_no) AS item_count
  FROM sample_table
 GROUP BY 1;

在此处输入图像描述

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

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