如何通过sql begineer将group by转换为分区

时间:2019-12-23 09:51:37

标签: mysql

我的查询哪个分组依据效果很好

select uid,count(*) from user_details where uid  <> '' group by uid

尝试通过以下方式转换为分区 我的分区查询不符合预期

select uid,(count(uid) OVER ( PARTITION BY uid )) counts from user_details where uid <> ''

无错误消息输出不正确。

 uid, Count As |
 Dates_not_found, 1,192 |
 Dates_not_found, 1,192 |

1 个答案:

答案 0 :(得分:1)

使用DISTINCT:查询会将计数结果的输出写入每一行。如果从中获得唯一的行,则需要使用DISTINCT来获得没有重复的结果。

FIDDLE DEMO HERE

SELECT  DISTINCT uid,(count(uid) OVER (PARTITION BY uid)) Counts 
FROM user_details 
WHERE uid  <> ''