给出一个表格,例如:
| ID | Value |
|-------------|
| 1 | "some" |
| 1 | "some" |
| 1 | "value"|
| 2 | "some" |
| 3 | "some" |
| 3 | "value |
| 3 | "value |
如何计算每个ID的唯一值出现次数?
所以您最终得到一个表,例如:
| ID | Value | number |
|-------------|--------|
| 1 | "some" | 2 |
| | "value"| 1 |
| 2 | "some" | 1 |
| 3 | "some" | 1 |
| | "value | 2 |
我尝试使用OVER(PARTITION BY ID order by Value)
通过ID分隔表并计算单独的值。但是,这会计算唯一出现的次数,然后将它们加在一起。所以我最终得到一个表,例如:
| ID | Value | number |
|-------------|--------|
| 1 | "some" | 2 |
| 1 | "some" | 2 |
| 1 | "value"| 3 |
| 2 | "some" | 1 |
| 3 | "some" | 1 |
| 3 | "value | 3 |
| 3 | "value | 3 |
是否有一种方法可以像我给出的第二个示例那样计算唯一值?
答案 0 :(得分:2)
以下是用于BigQuery标准SQL
#standardSQL
SELECT id, value, COUNT(1) number
FROM `project.dataset.table`
GROUP BY id, value
有结果
Row id value number
1 1 some 2
2 1 value 1
3 2 some 1
4 3 value 2
5 3 some 1