鉴于我有以下两个表:
User
-------
UserID
UserLikes
----------
UserID
OtherUserID
当用户喜欢其他用户时,执行该用户的用户将被存储为UserID,而接收该用户的用户将被存储为OtherUserID。
如何获得显示已提供给用户数量的分布:
0喜欢
1-10喜欢
11-50喜欢
51-100喜欢
101 +喜欢
答案 0 :(得分:0)
例如,喜欢1到10岁的人:
SELECT T.userID,count(*) as TotalLikes
FROM UserLikes t
GROUP BY T.userID
HAVING COUNT(*) between 1 and 10
要更改范围,只需更改having子句
即可HAVING COUNT(*) = 0
HAVING COUNT(*) between 1 and 10
HAVING COUNT(*) between 11 and 50
HAVING COUNT(*) between 51 and 100
HAVING COUNT(*) > 100
如果您的意思是希望结果如下:
0 likes | 1-10 likes | ....
3 5 ....
然后:
SELECT sum(case when TotalLikes = 0 then 1 else 0 end) as No_Likes,
SELECT sum(case when TotalLikes between 1 and 10 then 1 else 0 end) as One_To_ten_likes,
SELECT sum(case when TotalLikes between 11 and 50 then 1 else 0 end) as Eleven_To_Fifty_Likes,
.......
FROM(
SELECT T.userID,count(*) as TotalLikes
FROM UserLikes t
GROUP BY T.userID)