简体   繁体   中英

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. As i have 3 tables in my DB and i am trying to calculate sale from them with respect to time (Yearly sale). where i need to GROUP BY my query by date_added . In MYSQL it worked fine and give me fine result but in redshift i am stuck.

MYSQL QUERY :

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;

POSTGRES Query :

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

Your ORDER BY clause has o.date_added in it but your actual result set does not have it. The ORDER BY is done after the query is done.

You can use:

ORDER BY month asc, year asc LIMIT 25

Also, you can remove the extra parentheses from the GROUP BY :

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

DB-Fiddle

See Redshift documentation for use of now() function. Use getdate() instead, as the now() seems to be deprecated .

A column in an ORDER BY clause in a query containing GROUP BY works as if it were mentioned in the SELECT clause.

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 )

What you want, to get a query that works in multiple DBMSs, is this instead.

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.

I found the ERROR.I used SQL Workbench and get the Error about NOW() function that i was using in my POSTGRE query.

Simply, i juts replaced NOW() with CURRENT_DATE and things worked for me. Also, i get Error for LIMIT 0, 25 but in POSTGRE, they allow 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. IT DO NOT BERIEF ABOUT ERROR. I RECOMMEND TO USE SQL WORKBENCH. IT EXPLAIN ABOUT ERROR.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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