I am trying to use group by clause in subquery which is in from clause
select userID,count(id)
from
(
(
select id,max(bidAmount),userID
from Bids
group by id,bidAmount
)
group by userID
);
but this gives error
Error: near "group": syntax error
Is it possible to use group by clause in subquery in from clause in sql?
The GROUP BY command can be used to perform the same function as the ORDER BY in a subquery. Subqueries that return more than one row can only be used with multiple value operators such as the IN operator.
Subqueries cannot manipulate their results internally, that is, a subquery cannot include the order by clause, the compute clause, or the into keyword. Correlated (repeating) subqueries are not allowed in the select clause of an updatable cursor defined by declare cursor.
GROUP BY clause is used with the SELECT statement. In the query, GROUP BY clause is placed after the WHERE clause. In the query, GROUP BY clause is placed before ORDER BY clause if used any. In the query , Group BY clause is placed before Having clause .
Multiple Row Sub Query Multiple-row subqueries are nested queries that can return more than one row of results to the parent query. Multiple-row subqueries are used most commonly in WHERE and HAVING clauses. Since it returns multiple rows, it must be handled by set comparison operators (IN, ALL, ANY).
Check your (), they are not at right places. Should be something more like this:
select w.userID,count(w.id)
from (select id,max(bidAmount),userID from Bids group by id, userID) w
group by w.userID
Try this:
select userID,count(id)
from (
select id,max(bidAmount),userID from Bids group by id,userID
) as tmp
group by userID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With