简体   繁体   English

如何在SQL中按天计算出现次数?

[英]How do I count occurrences by day in SQL?

Newbie SQL question here --> 新手SQL问题在这里 - >

I've got an Occurrences table that contains a row for each time a user did something. 我有一个Occurrences表,每次用户做某事时都会包含一行。 A user can do the thing multiple times per day. 用户可以每天多次做这件事。 It looks like this: 它看起来像这样:

Date      Username
------    --------
1/1/9     User1
1/1/9     User1
1/1/9     User2
1/2/9     User1
1/2/9     User3
1/3/9     User1
1/3/9     User1
1/3/9     User1
1/3/9     User2
1/3/9     User3

I want a query to simply return the Username and the quantity (count) of unique days they appear. 我想要一个查询只返回用户名和它们出现的唯一天数(数量)。 For the above data set, the result I'm looking for would be: 对于上面的数据集,我正在寻找的结果是:

Username    UniqueDaysAppeared
--------    ------------------
User1       3
User2       2
User3       2

I keep getting screwed up because my query is returning not the count of unique days per user but rather the number of occurrences of the user overall. 我一直搞砸了,因为我的查询返回的不是每个用户的唯一天数,而是整个用户的出现次数。

SELECT Username, COUNT(DISTINCT(Date)) AS UniqueDaysAppeared
FROM Occurrences
GROUP BY Username

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM