Click here to Skip to main content
15,124,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Since it is sum, I get a single group function error when I try to combine it. How can I combine this query?

Query1:

SELECT DISTINCT A.Name,A.Code,B.Mount FROM SC A, PC B
WHERE A.Mounth=1 and A.Name='Green' order by B.Mount

Query2:

SELECT SUM(Values1) AS TOTAL FROM SC WHERE A.Mount=1 And A.Name='Green'

What I have tried:

I try query 1 left join query to but I take error single group function error. Same way query 2 left join query 3 and I take single group error.


I want to combine these many queries.
how can I do it
Posted
Updated 10-Aug-21 21:56pm
v3
Comments
Wendelius 10-Aug-21 14:38pm
   
Could you post some example data how the result should look like.
Member 14169626 10-Aug-21 15:58pm
   
Hi.

Result column 1: name (3 row green)
Result column 2: mount ( 3 row 1)
Result column 3: values(3 row values 1,6,3)

Sum is green 3 row: 1+6+3=10 group by green and mounth:1
Wendelius 10-Aug-21 16:51pm
   
Not sure I still understand. Query2, query3 and query4, they all return only one row and a single value. On the other hand query1 returns multiple rows.

Can you post, what each query returns when run alone and how would you like to combine these results (the final result set)?
Jörgen Andersson 17-Aug-21 3:30am
   
Query 1 doesn't have any join conditions between SC and PC. Is that correct?

1 solution

Maybe you just want to do this?
SQL
SELECT * FROM (VALUES ((query 1), (query 2), (query 3), (query 4))) AS t(q1, q2, q3, q4);
If so, you can find more examples at Microsoft official documentation (Table Value Constructor (Transact-SQL) - SQL Server | Microsoft Docs[^]).

Or maybe just this?
SQL
query 1 UNION ALL query 2 UNION ALL query 3 UNION ALL query 4;
If so, you can find more examples at Microsoft official documentation (UNION (Transact-SQL) - SQL Server | Microsoft Docs[^]).

It depends on whether you want it vertically or horizontally. By the way, please make sure that your first query returns a scalar like all others.
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900