0

我有一个简单的表格,其中包含 2013 年 10 月首次登录网站的 3 个用户。下表显示了每个用户的个人登录。在这种情况下,最小登录日期是他们首次登录的日期。

| user | login_date |
  user1    10/3
  user2    10/3
  user1    10/4
  user3    10/14
  user2    10/15
  user3    10/20

因此,要查找在 10 月 3 日首次登录的用户,我使用

SELECT user FROM table GROUP BY user HAVING MIN(login_date)='10/3';

但是,我如何计算那些用户,输出为 2?那么,拥有 login_date='10/3' 的 COUNT 个唯一用户?

4

2 回答 2

2

You can use a subquery:

select count(*)
from (SELECT user
      FROM table
      GROUP BY user
      HAVING MIN(login_date) = '10/3'
     ) t

Your format for a date constant leaves something to be desired. You might consider something like '2013-10-03', which should work in most databases.

于 2013-11-01T02:16:50.870 回答
1

How about

SELECT count(distinct(user)) FROM table GROUP BY user HAVING MIN(login_date)='10/3';

于 2013-11-01T02:16:27.677 回答