简体   繁体   English

无效操作:列“[column_name]”必须出现在 GROUP BY 子句中或在聚合 function 中使用;

[英]Invalid operation: column “[column_name]” must appear in the GROUP BY clause or be used in an aggregate function;

I have read almost 10+ questions related to mine but no one worked in my case.我已经阅读了近 10 多个与我相关的问题,但没有人在我的情况下工作。 As i have 3 tables in my DB and i am trying to calculate sale from them with respect to time (Yearly sale).因为我的数据库中有 3 张表,我正在尝试根据时间计算它们的销售额(年度销售额)。 where i need to GROUP BY my query by date_added .我需要按date_added对我的查询进行GROUP BY In MYSQL it worked fine and give me fine result but in redshift i am stuck.MYSQL ,它工作得很好并且给了我很好的结果,但是在红移中我被卡住了。

MYSQL QUERY : MYSQL 查询

SELECT
MONTHNAME(o.date_added) AS MONTH,
YEAR(o.date_added) AS YEAR,
COUNT(o.order_id) AS orders,
FROM
order o
LEFT JOIN(
SELECT
    op.order_id,
    SUM(op.quantity) AS op_qty,
    SUM(op.total) AS total,
    SUM(op.cost) AS cost
FROM
    order_product op
GROUP BY
    op.order_id
) op
ON
op.order_id = o.order_id
LEFT JOIN(
SELECT
    order_id,
    SUM(IF(CODE = 'coupon', VALUE, 0)) AS coupon
FROM
    order_total
WHERE
    1
GROUP BY
    order_id
) ot
ON
ot.order_id = o.order_id
WHERE
(
    DATE(o.date_added) >= DATE_ADD(NOW(), INTERVAL - 24 MONTH) AND DATE(o.date_added) <= 
DATE(NOW())) AND(o.order_status_id = '22' OR o.order_status_id = '23')
GROUP BY
    MONTH(o.date_added),
    YEAR(o.date_added)
ORDER BY
    date_added ASC
LIMIT 0, 25

This MYSQL query working very fine but when i convert it to RedShift's POSTGRE format it gives me error of Invalid operation: column "o.date_added" must appear in the GROUP BY clause or be used in an aggregate function;这个 MYSQL 查询工作得很好,但是当我将它转换为 RedShift 的 POSTGRE 格式时,它给了我Invalid operation: column "o.date_added" must appear in the GROUP BY clause or be used in an aggregate function;

POSTGRES Query : POSTGRES 查询

SELECT
EXTRACT(MONTH FROM o.date_added) AS month,
EXTRACT(YEAR FROM o.date_added) AS year,
COUNT(o.order_id) AS orders
FROM
orders o  
LEFT JOIN
(
    SELECT
        op.order_id,
        SUM(op.quantity) AS op_qty,
        SUM(op.total) AS total,
        SUM(op.cost) AS cost            
    FROM
        order_product op 
    GROUP BY
        op.order_id
) op 
    ON op.order_id = o.order_id             
LEFT JOIN
(
    SELECT
        order_id,
        SUM(CASE 
            WHEN CODE = 'coupon' THEN VALUE 
            ELSE 0 
        END) AS coupon                      
    FROM
        order_total 
    WHERE
        1 
    GROUP BY
        order_id
) ot 
ON ot.order_id = o.order_id                        
WHERE
(
    DATE(o.date_added) >= now() + interval '-24 month' 
    AND DATE(o.date_added) <= DATE(NOW())
)                               
AND (
    o.order_status_id = '22' 
    OR o.order_status_id = '23'
)                               
GROUP BY
(EXTRACT(MONTH FROM o.date_added), EXTRACT(YEAR FROM o.date_added))
ORDER BY
o.date_added ASC LIMIT 25

Is there any syntax error in postgre query and also why i need to add o.date_added in GROUP BY postgre 查询中是否有任何语法错误以及为什么我需要在GROUP BY中添加o.date_added

Your ORDER BY clause has o.date_added in it but your actual result set does not have it.您的ORDER BY子句中有o.date_added ,但您的实际结果集中没有它。 The ORDER BY is done after the query is done. ORDER BY在查询完成后完成。

You can use:您可以使用:

ORDER BY month asc, year asc LIMIT 25

Also, you can remove the extra parentheses from the GROUP BY :此外,您可以从GROUP BY中删除额外的括号:

GROUP BY EXTRACT(MONTH FROM o.date_added), EXTRACT(YEAR FROM o.date_added)

DB-Fiddle DB-小提琴

See Redshift documentation for use of now() function.有关now() function 的使用,请参阅 Redshift 文档 Use getdate() instead, as the now() seems to be deprecated .请改用getdate() ,因为now()似乎已被弃用

A column in an ORDER BY clause in a query containing GROUP BY works as if it were mentioned in the SELECT clause.包含GROUP BY的查询中的ORDER BY子句中的列的工作方式与SELECT子句中提到的一样。

You have你有

SELECT
MONTHNAME(o.date_added) AS MONTH,
YEAR(o.date_added) AS YEAR,
COUNT(o.order_id) AS orders,
FROM
order o 
...
GROUP BY
    MONTH(o.date_added),
    YEAR(o.date_added)
ORDER BY
    date_added ASC
LIMIT 0, 25

In MySQL this takes advantage of its notorious nonstandard handling of GROUP BY (read this. https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html ) In MySQL this takes advantage of its notorious nonstandard handling of GROUP BY (read this. https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html )

What you want, to get a query that works in multiple DBMSs, is this instead.你想要的是得到一个在多个 DBMS 中工作的查询,而不是这个。

ORDER BY
    MONTH(o.date_added) ASC,
    YEAR(o.date_added) ASC   

Adding o.date_added to your GROUP BY clause is incorrect, because you are grouping not by the entire date, but by the month and year of the date.o.date_added添加到您的GROUP BY子句是不正确的,因为您不是按整个日期分组,而是按日期的月份和年份分组。

I found the ERROR.I used SQL Workbench and get the Error about NOW() function that i was using in my POSTGRE query.我发现了错误。我使用了 SQL 工作台并得到关于我在POSTGRE查询中使用的NOW() function 的错误。

Simply, i juts replaced NOW() with CURRENT_DATE and things worked for me.简单地说,我用CURRENT_DATE替换了NOW()并且事情对我有用。 Also, i get Error for LIMIT 0, 25 but in POSTGRE, they allow LIMIT 25 [OFFSET n] .另外,我得到LIMIT 0, 25的错误,但在 POSTGRE 中,它们允许LIMIT 25 [OFFSET n]

Now my query looks like:现在我的查询看起来像:

SELECT
EXTRACT(MONTH FROM o.date_added) AS month,
EXTRACT(YEAR FROM o.date_added) AS year,
COUNT(o.order_id) AS orders
FROM
orders o  
LEFT JOIN
(
    SELECT
        op.order_id,
        SUM(op.quantity) AS op_qty,
        SUM(op.total) AS total,
        SUM(op.cost) AS cost            
    FROM
        order_product op 
    GROUP BY
        op.order_id
) op 
    ON op.order_id = o.order_id             
LEFT JOIN
(
    SELECT
        order_id                       
    FROM
        order_total 
    WHERE
        1 
    GROUP BY
        order_id
) ot 
    ON ot.order_id = o.order_id                        
WHERE
(
    DATE(o.date_added) >= CURRENT_DATE + interval '-24 month' 
    AND DATE(o.date_added) <= DATE(CURRENT_DATE)
)                              
GROUP BY EXTRACT(MONTH FROM o.date_added), EXTRACT(YEAR FROM o.date_added)
ORDER BY year ASC, month ASC LIMIT 25

NOTE: IF YOU ARE WORKING ON REDSHIFT.注意:如果您正在使用 REDSHIFT。 IT DO NOT BERIEF ABOUT ERROR.它不相信错误。 I RECOMMEND TO USE SQL WORKBENCH.我建议使用 SQL 工作台。 IT EXPLAIN ABOUT ERROR.它解释了错误。

暂无
暂无

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

相关问题 SQL:列必须出现在 GROUP BY 子句中或在聚合函数中使用 - SQL: Column Must Appear in the GROUP BY Clause Or Be Used in an Aggregate Function postgres-SELECT / GROUP BY中的部分列-列必须出现在GROUP BY子句中或在聚合函数中使用 - postgres - partial column in SELECT/GROUP BY - column must appear in the GROUP BY clause or be used in an aggregate function PGError:错误:列“ inboxes.id”必须出现在GROUP BY子句中或在聚合函数中使用 - PGError: ERROR: column “inboxes.id” must appear in the GROUP BY clause or be used in an aggregate function 列“d.discount_amount”必须出现在 GROUP BY 子句中或用于聚合函数中 - column “d.discount_amount” must appear in the GROUP BY clause or be used in an aggregate function SQL:必须出现在GROUP BY子句中或在聚合函数中使用 - SQL: Must appear in the GROUP BY clause or be used in an aggregate function 必须出现在GROUP BY子句中或在聚合函数中使用(PostgreSQL) - Must appear in the GROUP BY clause or be used in an aggregate function (PostgreSQL) mysql 中“on 子句”中的未知列“column_name” - Unknown column 'column_name' in 'on clause' in mysql MySQL:在列列表中以及在HAVING子句中再次使用COUNT(column_name)。 这会导致COUNT(column_name)操作运行两次吗? - MySQL: Using COUNT(column_name) in the column list, and again in the HAVING clause. Does this cause the COUNT(column_name) operation to run twice? invalid:聚合函数或GROUP BY子句 - invalid : aggregate function or the GROUP BY clause MySQL:“where子句中的列'column_name'是不明确的” - MySQL: “Column 'column_name' in where clause is ambiguous”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM