group by 语句期间行值之间的数学运算

Posted

技术标签:

【中文标题】group by 语句期间行值之间的数学运算【英文标题】:math operations between row values during group by statement 【发布时间】:2021-03-08 03:28:08 【问题描述】:

在这个previous question 中,我试图按照目前的方式对我的产品进行分组。

现在我正在尝试将我查询的所有产品与所有已售产品的总和以及每种情况下的平均价格进行分组,这就是它的外观:

这是我的查询:

select 
        missions_answer.response_id as Response,
        crm_player."document" as Document,
        MAX(missions_answerselectmultiple.body::json ->> 'name') FILTER (WHERE missions_question.label = 'Producto') as Product,
        MAX(missions_answerinteger.body) FILTER (WHERE missions_question.label = 'Cantidad unidades vendidas') as Amount,
        MAX(missions_answerinteger.body) FILTER (WHERE missions_question.label = 'Valor Unidad') as Price
from missions_answer 
    left join missions_question on missions_answer.question_id = missions_question.id 
    left join missions_answertext on missions_answer.id = missions_answertext.answer_ptr_id 
    left join missions_answerselectmultiple on missions_answer.id = missions_answerselectmultiple.answer_ptr_id
    left join missions_answerinteger on missions_answer.id = missions_answerinteger.answer_ptr_id 
    left join missions_response on missions_answer.response_id = missions_response.id
    left join crm_player on missions_response.player_id = crm_player.id
    LEFT JOIN crm_user ON crm_player.user_id = crm_user.id
    group by Response, Document

我试图得到这样的东西:

+ ----------------------- + ---------- + ---------- + 
-       product           + total_sold + mean_price +
+ ----------------------- + ---------- + ---------- + 
+ Perfiles- Esquinera.... +    669.000 +    2.500   +
+ Traslucent...           +   900.000  +    30.000  +

对于表格中的所有产品,以此类推,事实是我无法将所有价格与分组期间的金额相乘,也无法计算表格中每个产品案例的平均价格。

我正在尝试这个:

select 
        MAX(missions_answerselectmultiple.body::json ->> 'name') FILTER (WHERE missions_question.label = 'Producto') as Product,
        MAX(missions_answerinteger.body) FILTER (WHERE missions_question.label = 'Cantidad unidades vendidas') as Amount,
        MAX(missions_answerinteger.body) FILTER (WHERE missions_question.label = 'Valor Unidad') as Price,
        sum( (MAX(missions_answerinteger.body) FILTER (WHERE missions_question.label = 'Valor Unidad')) * (MAX(missions_answerinteger.body) FILTER (WHERE missions_question.label )= 'Cantidad unidades vendidas')  ) as Total
from missions_answer 
    left join missions_question on missions_answer.question_id = missions_question.id 
    left join missions_answertext on missions_answer.id = missions_answertext.answer_ptr_id 
    left join missions_answerselectmultiple on missions_answer.id = missions_answerselectmultiple.answer_ptr_id
    left join missions_answerinteger on missions_answer.id = missions_answerinteger.answer_ptr_id 
    left join missions_response on missions_answer.response_id = missions_response.id
    left join crm_player on missions_response.player_id = crm_player.id
    LEFT JOIN crm_user ON crm_player.user_id = crm_user.id
    group by Product

但我明白了

SQL Error [42803]: ERROR: aggregate function calls cannot be nested

希望有人可以帮助我,提前感谢任何提示或帮助。

【问题讨论】:

有什么问题? sum(axb) 应该可以工作。如果需要,删除 NULL 值。 【参考方案1】:

我认为您需要另一个级别的聚合:

select product, sum(amount) as total_sold, avg(price) as mean_price
from (
    select 
        ma.response_id as response,
        cp."document" as document,
        max(mas.body::json ->> 'name') filter(where mq.label = 'Producto') as Product,
        max(mai.body) filter(where mq.label = 'Cantidad unidades vendidas') as Amount,
        MAX(mai.body) filter(where mq.label = 'Valor Unidad') as Price
    from missions_answer ma
    left join missions_question             mq  on ma.question_id = mq.id 
    left join missions_answertext           mat on ma.id = mat.answer_ptr_id 
    left join missions_answerselectmultiple mas on ma.id = mas.answer_ptr_id
    left join missions_answerinteger        mai on ma.id = mai.answer_ptr_id 
    left join missions_response             mr  on ma.response_id = mr.id
    left join crm_player                    cp  on mr.player_id = cp.id
    left join crm_user                      cu  on cp.user_id = cu.id
    group by response, document
) t
group by product

请注意,我将查询更改为对所有表使用表别名;这会缩短代码,并使其更易于理解。

【讨论】:

有效,非常感谢您的回答!但是作为total_sold,我指的是amount * price的乘法,试图在第一行用sum(amount * price)替换sum(amount),但我得到ERROR: integer out of range,你知道吗?

以上是关于group by 语句期间行值之间的数学运算的主要内容,如果未能解决你的问题,请参考以下文章

使用“group by”获取正确的行值 [重复]

在pyspark数据框中根据group by连接行值

子查询GROUP BY运算符

DataTable Group By或运算 Linq Aggregate的使用

group by后接的having语句怎样使用才是有效的,我为啥不行的

如何在 Laravel 8 中使用 PostgreSQL 中的子查询通过 group by 子句获取行值?