[英]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)
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.