【问题标题】:In Access,get the Max time and min time using sql在Access中,使用sql获取最大时间和最小时间
【发布时间】:2016-05-26 02:29:35
【问题描述】:

我的表就是这样,我想得到Maxtime和Mintime,当user-id是A或者B时。

user_id        time
----------   ----------

A           2016-01-03 23:02:35
A           2016-01-03 23:02:36
A           2016-01-03 23:02:38
B           2016-01-03 22:02:35
B           2016-01-03 22:02:39

我的代码是:

 SELECT user_id,Max(time)AS [start], Min(time) AS [end]
 FROM client1
 WHERE user_id is not null
 GROUP BY user_id
 HAVING user_id in (select user_id from client1);

我用access作为数据库,像这样 search

【问题讨论】:

  • 你的Havingclause 毫无意义,不是吗?
  • 你的意思是“HAVING client1.user_id in (select client1.user_id from client1)”
  • 我的意思是你可以删除它。让数据库检查GROUP BY 中的user_id 是否在same 表的所有user_id 列表中是没有意义的。情况总是如此。

标签: sql ms-access select max min


【解决方案1】:

我的猜测是下面的选择应该返回所需的结果:

SELECT user_id,Max(time)AS [start], Min(time) AS [end]
 FROM client1
 WHERE user_id IN ('A', 'B', 'C', 'D', 'Any other value')
 GROUP BY user_id

注意: 要获取 所有用户的最小和最大时间,只需删除 WHERE 子句:

SELECT user_id,Max(time)AS [start], Min(time) AS [end] FROM client1 
    GROUP BY user_id

【讨论】:

  • A 或 B 只是一个例子。有很多user_id,比如C,D,E。
  • 但是'Any other value'的数量是一千个?
  • 您从哪里获得 id 列表?从另一个表?
  • 没有,只是在表中,user_id的个数很大。
  • 那么如果要获取所有的user_id就不需要加上where子句(去掉过滤器就行了)
猜你喜欢
  • 1970-01-01
  • 2012-12-21
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
  • 1970-01-01
  • 2017-05-12
相关资源
最近更新 更多