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

标签 sql postgresql

我有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/

相关文章:

postgresql - 新的 3x 版本不需要 mono.security.dll 吗?

python - 使用 Psycopg2 时,Postgres 在查询期间在几百秒后关闭连接

mysql - ORDER BY ... ASC 很慢, "Using index condition"

sql - 如何在Powershell中遍历数据表?

sql - Groupby排序和单行选择

ruby-on-rails - Rails 5 - 查询具有 uniq 属性的记录

sql - Azure 数据工厂 V2 : How to pass a file name to stored procedure variable

SQL删除查询优化

python - SQL LIKE 多个值

sql - PostgreSQL 不会使用主键进行内部连接