-1

使用 groupBy 子句时是否可以显示所有表行?经验:

id      user_id   gift_id  
 1        1          1     
 2        1          2     
 3        1          1    
 4        2          3    
 5        2          1    
 6        3          4    

预期结果:(按user_id计算后)

id      user_id   gift_id   count 
 1        1          1       3
 2        1          2       3
 3        1          1       3
 4        2          3       2
 5        2          1       2
 6        3          4       1
4

2 回答 2

2

尝试这个

select a.*, b.user_count from tablename as a join 
(select user_id, count(*) as user_count from tablename group by user_id) as b on
a.user_id= b.user_id 
于 2013-11-07T10:14:21.440 回答
0
SELECT id
     , user_id
     , gift_id
     , Count(user_id) over (PARTITION BY user_id) nb_user_id
FROM matable
于 2013-11-07T10:17:11.437 回答