SQL Server Cannot perform an aggregate function on an expression containing an aggregate or a subquery

0 votes

Error:

Cannot perform an aggregate function on an expression containing an aggregate or a subquery

The part where the error occurred:

SELECT column_1, column_2,
       SUM(CASE WHEN column_2 NOT IN (SELECT product FROM table_products) THEN 1
                ELSE 0
           END) AS Total
FROM my_table 
WHERE is_rated = '1'
GROUP BY column_1, column_2 

Can someone please help me with this?

Aug 18, 2022 in Database by Kithuzzz
• 38,000 points
1,508 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

In general, if you strive to prevent correlated subqueries, your performance will be substantially better:

SELECT
    MT.column_1,
    MT.column_2,
    SUM(CASE WHEN P.product IS NULL THEN 1 ELSE 0 END) AS total
FROM
    My_Table MT
LEFT OUTER JOIN Products P ON P.product = MT.column_2
WHERE
    MT.is_rated = '1'
GROUP BY
    MT.column_1,
    MT.column_2

This is based on the supposition that there will never be more than one match in the Products table (Products, not Table Products – it's a table, so don't call it that). In other words, if the product is the PK (or an AK) of the Products table, then this will work.

If not, and there may be several matches in the Products database, you can JOIN to a subquery that makes use of the DISTINCT operator on the product field.

answered Aug 19, 2022 by narikkadan
• 63,600 points

edited Mar 5
0 votes
need answer for this
answered Mar 15, 2023 by anonymous

edited Mar 5

Related Questions In Database

0 votes
1 answer
0 votes
1 answer

How to run a SQL query on an Excel table?

On Excel tables, how to construct and ...READ MORE

answered Apr 11, 2022 in Database by gaurav
• 23,260 points
1,178 views
0 votes
0 answers

Solutions for INSERT OR UPDATE on SQL Server

Consider the MyTable table structure. I frequently ...READ MORE

Aug 27, 2022 in Database by Kithuzzz
• 38,000 points
830 views
0 votes
0 answers
0 votes
0 answers
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3, 2022 in Database by Vaani
• 7,070 points
944 views
0 votes
0 answers

How do I UPDATE from a SELECT in SQL Server?

INSERT INTO Table (col1, col2, col3) SELECT col1, ...READ MORE

Feb 4, 2022 in Database by Vaani
• 7,070 points
626 views
0 votes
1 answer

What is a stored procedure?

A stored procedure is a set of ...READ MORE

answered Feb 4, 2022 in Database by Neha
• 9,020 points
1,184 views
0 votes
1 answer

LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

At the top level there are mainly ...READ MORE

answered Feb 4, 2022 in Database by Neha
• 9,020 points
2,095 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP