简体   繁体   English

Bigquery SQL - 出现两次的数组值计数

[英]Bigquery SQL - Count of array values that appear twice

I have an array of ID values and I wish to count the number of values that appear exactly twice in each array.我有一个 ID 值数组,我想计算每个数组中恰好出现两次的值的数量。 Below is an example of the data and desired outcome:以下是数据和预期结果的示例:

在此处输入图像描述

I have the following code that can count the total (or could count the distinct total) of values but I can't figure out how I need to modify it so that it only counts the ID's that appear twice.我有以下代码可以计算值的总数(或可以计算不同的总数)但我无法弄清楚我需要如何修改它以便它只计算出现两次的 ID。

SELECT mth_end_date, 
  (SELECT COUNT(id) FROM UNNEST(SPLIT(user)) AS id) segment_count
FROM temp2 

I know I am missing something really obvious here, but I just can't crack it.我知道我在这里遗漏了一些非常明显的东西,但我就是无法破解它。

Appreciate your suggestions!感谢您的建议!

Consider below approach考虑以下方法

在此处输入图像描述

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

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

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