繁体   English   中英

出现在 Group By 子句中或在聚合函数中使用

[英]Appear in the Group By Clause or use in an aggregate function

计算第一个实际购买的项目并填充 tr2_invoice 中的 first_actual_item 列。

SELECT cust_id, total_amount, items, MIN(time_in) 
FROM tr_invoice WHERE total_amount <> 0 
GROUP BY cust_id;

错误:列“tr_invoice.total_amount”必须出现在 GROUP BY 子句中或用于聚合函数第 1 行:SELECT cust_id、total_amount、items、MIN...

我使用 AVG()、MIN()、MAX() 或 ARRAY_AGG() 作为total_amountitems的聚合,它的输出与我在 MySQL 上查询的不同。 有没有更好的解决方案来解决这个问题?

所选字段必须出现在 GROUP BY 子句中。

1.

SELECT cust_id, total_amount, items, MIN(time_in) over( PARTITION by cust_id) as min_time_in FROM tr_invoice WHERE total_amount <> 0 ;

2.

SELECT
    b.cust_id     ,
    b.total_amount,
    b.items       ,
    a.min_time_in
from
    (
        SELECT
            cust_id,
            MIN(time_in) as min_time_in
        FROM
            tr_invoice 
        WHERE
            total_amount <> 0
        GROUP BY
            cust_id
    )a
    join
        tr_invoice b
        ON
            a.cust_id=b.cust_id;

请参考链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM