gpt4 book ai didi

sql - SQL中的表分析(乘法)

转载 作者:行者123 更新时间:2023-11-29 11:41:28 30 4
gpt4 key购买 nike

我有TABLE_A我需要创建 TABLE_A_FINAL

规则:
TABLE_A_FINAL中我们有包含 ID_C 的所有可能组合的行如果在 TABLE_AID_C 的组合相同我们乘以 WEIGHT 的值.
这在SQL 中可行吗?感谢您的帮助和建议。

<strong>TABLE_A</strong><br/>
<strong>ID_N |WEIGHT |ID_C |</strong>
1 |1.15 | 1A |
2 |1.13 | 1A |
3 |1.65 | 1B |
4 |1.85 | 2A |
6 |1.57 | 2A |


<strong>TABLE_A_FINAL</strong><br/>
<strong>ID_C |FINAL_WEIGHT |</strong>
1A |1.15×1.13 = <strong>1.2995</strong> |
1B |<strong>1.65</strong> |
2A |1.85×1.57 = <strong>2.9045</strong> |

最佳答案

不幸的是,Postgres 没有product() 聚合函数。您可以使用 sum() 和 logs/exponentiation 模拟这样的函数。因为您的值似乎都是正数且非零:

select id_c, exp(sum(ln(weight)) as final_weight
from table_a
group by id_c;

如果你有零或负数,那么逻辑会更复杂,但仍然很有可能。

关于sql - SQL中的表分析(乘法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200154/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com