gpt4 book ai didi

postgresql - Postgres 查询 GROUP BY

转载 作者:行者123 更新时间:2023-11-29 14:13:03 25 4
gpt4 key购买 nike

请帮我解决这个问题

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/

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