【发布时间】:2021-08-31 18:40:53
【问题描述】:
我有一个包含列 A、B、C 的数据集。我想对 C 求和,其中 B 中的值 >= 比 B 中的其余值。我尝试了 Sum 的情况,但不能t 似乎获得了使用行值的条件。有什么想法吗?
输入:
A|B|C
1|1|1
1|2|1
1|3|1
2|1|0
2|2|1
2|3|0
想要的输出:
A|B|C|Output
1|1|1|3
1|2|1|2
1|3|1|1
2|1|0|1
2|2|1|1
2|3|0|0
代码已尝试,但由于条件原因无法正常工作
SUM(case when B>=B then C end) over(partition by A) as Output
输出计算:
A|B|C|Output calculation |Excel calculation | Output
1|1|1|Sum all values in Col C where values in B>=1 and A=1 | =SUMIFS(C:C,B:B,">="&B2,A:A,A2) |=3
1|2|1|Sum all values in Col C where values in B>=2 and A=1 | =SUMIFS(C:C,B:B,">="&B3,A:A,A3) |=2
1|3|1|Sum all values in Col C where values in B>=3 and A=1 | =SUMIFS(C:C,B:B,">="&B4,A:A,A4) |=1
2|1|0|Sum all values in Col C if values in B>=1 and A=2 | =SUMIFS(C:C,B:B,">="&B5,A:A,A5) |=1
2|2|1|Sum all values in Col C if values in B>=2 and A=2 | =SUMIFS(C:C,B:B,">="&B6,A:A,A6) |=1
2|3|0|Sum all values in Col C if values in B>=3 and A=2 | =SUMIFS(C:C,B:B,">="&B7,A:A,A7) |=0
【问题讨论】:
-
您使用的是 Bigquery 还是 T-SQL?我现在已经删除了冲突的标签
-
什么是“其余值”? (对我来说)你的输出是如何计算的还不清楚。也许你可以展示一下这个细节。
-
“其余值” = col B 中的所有值
-
已添加 excel 公式,可能有助于解释更多信息 - 希望对您有所帮助
-
@16143 "rest of the values" = col B 中的所有值,表示仅当 b = max(b) 时才计算 sum(c)
标签: sql google-bigquery sum case