我有TABLE_A
我需要创建 TABLE_A_FINAL
规则:
在TABLE_A_FINAL
中我们有包含 ID_C
的所有可能组合的行如果在 TABLE_A
与 ID_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/