不同用户的行之间的最大日期差异

时间:2015-10-23 15:02:10

标签: mysql date datediff

选择一个简单的表格,如:

labelScanMode.Content = ps.GetScanMode().ToString();

我想计算用户连接之间的最大日期差异。

由于

2 个答案:

答案 0 :(得分:0)

几天......

select user_id, max(conn_date) - min(conn_date) from conn
group by user_id;

答案 1 :(得分:0)

尝试如下

select 
  mainqry.userid,
  max(mainqry.daysdiff) as max_date_diff
From
(
select 
  qry.userid,
  -- qry.id1,
  -- qry.currid,
  ( Case when qry.id1 = qry.currid then qry.dt1 else null end ) prevconndt,
  qry.currdt,
  datediff((Case when qry.id1 = qry.currid then qry.dt1 else null end),qry.currdt) daysdiff
from 
(
select 
  userid,
  @previd id1,
  @previd := userid as currid,
  @prevdt dt1,
  @prevdt := connectiondate as currdt
from table1,(select @prevdt:=NULL,@previd := NULL) a  
order by userid, connectiondate desc
) qry 
) MainQry
group by MainQry.userid;

它将在下面输出您的给定数据

Userid max_date_diff
1      4
2      5 
3      5 
4      Null 

你也可以使用下面的方法进行验证......

select 
qry.userid,
-- qry.id1,
-- qry.currid,
( Case when qry.id1 = qry.currid then qry.dt1 else null end ) prevconndt,
qry.currdt,
datediff((Case when qry.id1 = qry.currid then qry.dt1 else null end),qry.currdt) daysdiff
from 
(
select 
  userid,
  @previd id1,
  @previd := userid as currid,
  @prevdt dt1,
  @prevdt := connectiondate as currdt
from table1,(select @prevdt:=NULL,@previd := NULL) a  
order by userid, connectiondate desc
) qry