postgresql - Postgres 查询 GROUP BY

标签 postgresql odoo

请帮我解决这个问题

SELECT
   quant.id AS id,
   quant.product_id AS product_id,
   quant.location_id AS location_id,
   quant.qty AS qty,
   quant.lot_id AS lot_id,
   quant.package_id AS package_id,
   quant.in_date AS in_date,
   quant.company_id,
   prod.default_code,
   template.categ_id AS categ_id
FROM stock_quant AS quant
JOIN product_product prod ON prod.id = quant.product_id
JOIN product_template template ON template.id = prod.product_tmpl_id
WHERE quant.company_id = 1
AND location_id = 12

我想按 prod.default_codequant.product_id 对结果进行分组>

这是结果示例的屏幕截图。我要数量总和 enter image description here

我该怎么做?

最佳答案

如果您只想要列 default_code, product_id, sum_of_qty 那么您可以这样做:

SELECT
   prod.default_code,
   quant.product_id AS product_id,
   SUM(quant.qty) AS sum_of_qty
FROM stock_quant AS quant
JOIN product_product prod ON prod.id = quant.product_id
JOIN product_template template ON template.id = prod.product_tmpl_id
WHERE quant.company_id = 1
AND location_id = 12
GROUP BY prod.default_code, product_id

如果您只需要 default_codeproduct_id,只需从 GROUP BYSELECT 中删除不必要的字段>.

关于postgresql - Postgres 查询 GROUP BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58977936/

相关文章:

postgresql - 在 PostgreSQL 中更新引用表时如何违反重复键约束(Unique_Violation)?

sql - 如何制作 ORDER SIBLINGS BY?

xml - Odoo Qweb t 值字符串中的新行

python - Openerp 中 .rml 报告中的文字换行

php - Odoo 服务器超时和内存限制

ruby - ROR + 不同格式的 Ruby 日期

c++ - PostgreSQL无法访问

postgresql - 尽管存在角色名称但不删除角色

postgresql - 如何使用 Odoo 从两个不同的 PostgreSQL 角色登录同一个数据库?

odoo - 使用 XMLRPC 删除 Odoo 模型中的所有记录