简体   繁体   中英

using group by clause in subquery in sql

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?

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

You can use group by in a subquery, but your syntax is off.

select userID,count(id)  
from  
(
        select id,max(bidAmount),userID  
        from Bids  
        group by id,userID
)
GROUP BY userid

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