• Register
0 votes

Problem :

My stored procedure giving me following error

cannot perform an aggregate function on an expression containing an aggregate or a subquery.

6 5 3
6,930 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

You can move the your logic for SUM() inside the subquery as follows:

SELECT t.col_1, t.col_2,
       (SELECT COUNT(*)
        FROM table_prods p
        WHERE t.col_2 <> p.prod
       ) as Total
FROM my_table t
WHERE t.is_rated = '1'
GROUP BY t.col_1, t.col_2 ;
This will surely resolve your issue.
9 7 4
38,600 points

Related questions

0 votes
1 answer 8 views
Problem I am working on some query for my project and the requirement is to show the second value. For sake of example, say I have salary table in Microsoft SQL Server and I need fetch the second maximum salary. I am not too experienced in T-SQL, I used the ... So I sow there is RANK() function in T-SQL, but I do not completely understand how it works. Any suggestions will very appreciated.
asked Dec 17, 2020 alexh 2.2k points
0 votes
2 answers 519 views
Problem : I want to import the flat file into my oledb target sql server database. But I am getting below error message : [Source - 18942979103_txt [424]] Error: Data conversion failed. The data conversion for column "recipient-name" returned status value 4 and status text ... more characters had no match in the target code page.". Why am I getting this error message? What am I doing wrong here?
asked Dec 23, 2019 alecxe 7.5k points
0 votes
1 answer 17 views
0 votes
2 answers 119 views
Problem : I am new to the SQL. What am I doing wrong here? SELECT LEFT(SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%', batchinfo.datapath), 8000), PATINDEX('%[^0-9]%', SUBSTRING(batchinfo.datapath, PATINDEX('%[0-9][0-9][0-9]%',batchinfo. ... I am trying to do group by the first, second, and third columns having the max rid. It is works perfectly fine without the group by and having.
asked Nov 27, 2019 alecxe 7.5k points
0 votes
1 answer 9 views
0 votes
1 answer 37 views
0 votes
1 answer 9 views
1 vote
1 answer 131 views
Problem : I am very new to SQL. If I try to use the sp_executesq I am often facing below strange looking error: “Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.” I have spent more than two days on above error but still can not fix above sql related error. Please guide me in fixing above sql error.
asked May 12, 2020 Martin K 6.6k points
0 votes
1 answer 455 views
Problem : I have the table storing starttime and endtime. I am facing below error: &ldquo;Conversion failed when converting character string to smalldatetime data type.&rdquo; While selecting the below query using that table. select CAST(substring(CONVERT(varchar,convert(smalldatetime,o. ... face any error. But, when I select it from a table I got the error. Can somebody help me with this?
asked Jan 20, 2020 jwilliam 3.9k points