Sql 显示最近的日期,而不是其他日期

Sql 显示最近的日期,而不是其他日期,sql,Sql,谢谢你的关注 我试图让每位客户的访问间隔时间最小。使用下面的代码,我可以得到每次访问之间的时间量,但它会比较每个日期。我只需要最小的边距,然后去掉/隐藏所有其他的边距 select a.account_id, a.transaction_id, b.transaction_date , a.transaction_date, round(a.transaction_date - b.transaction_date, 0) as Time_between from time

谢谢你的关注

我试图让每位客户的访问间隔时间最小。使用下面的代码,我可以得到每次访问之间的时间量,但它会比较每个日期。我只需要最小的边距,然后去掉/隐藏所有其他的边距

select a.account_id, a.transaction_id, b.transaction_date , a.transaction_date,            round(a.transaction_date - b.transaction_date, 0) as Time_between
from time_between_trans a, time_between_trans b
where a.transaction_date > b.transaction_date
and a.account_ID like '717724'
and a.account_id = b.account_id
对于事务处理中的每个日期,请在 运输

它们必须具有相同的帐户id,并且第二个日期必须晚于 比第一个


您需要向查询中添加分组依据:

select a.account_id, a.transaction_id, min(time_between) as min_time_between
from (select a.account_id, a.transaction_id, b.transaction_date, a.transaction_date,
             round(a.transaction_date - b.transaction_date, 0) as Time_between
      from time_between_trans a join
           time_between_trans b
           on a.transaction_date > b.transaction_date and
              a.account_ID ='717724' and
              a.account_id = b.account_id
     ) a
group by a.account_id, a.transaction_id
我还修复了您的连接语法以使用正确的连接语法,并将like更改为an=,因为您没有通配符

可能还有其他方法来表达这一点,但这是标准的SQL。您应该指定问题中使用的数据库

如果您使用的是Oracle,则只需执行以下操作:

select a.*,
       (case when nextdt - transaction_date) < transaction_date - nextdt
             then nextdt - transaction_date)
             else transaction_date - nextdt
        end) as mintime
from (select a.*,
             lead(transaction_date, 1) over (partition by account_id order by transaction_date) as nexttd,
             lag(transaction_date, 1) over (partition by account_id order by transaction_date) as prevtd
      from time_between_trans a
     ) a

实际上,这并不完全正确,因为您必须为nexttd和prevtd考虑null。但是,这个想法很简单。lead和lag函数允许您获取prev或text事务,然后您可以执行任何您想要查找最小值的操作,或者从记录中获取任何您想要的其他信息。

您需要在查询中添加分组依据:

select a.account_id, a.transaction_id, min(time_between) as min_time_between
from (select a.account_id, a.transaction_id, b.transaction_date, a.transaction_date,
             round(a.transaction_date - b.transaction_date, 0) as Time_between
      from time_between_trans a join
           time_between_trans b
           on a.transaction_date > b.transaction_date and
              a.account_ID ='717724' and
              a.account_id = b.account_id
     ) a
group by a.account_id, a.transaction_id
我还修复了您的连接语法以使用正确的连接语法,并将like更改为an=,因为您没有通配符

可能还有其他方法来表达这一点,但这是标准的SQL。您应该指定问题中使用的数据库

如果您使用的是Oracle,则只需执行以下操作:

select a.*,
       (case when nextdt - transaction_date) < transaction_date - nextdt
             then nextdt - transaction_date)
             else transaction_date - nextdt
        end) as mintime
from (select a.*,
             lead(transaction_date, 1) over (partition by account_id order by transaction_date) as nexttd,
             lag(transaction_date, 1) over (partition by account_id order by transaction_date) as prevtd
      from time_between_trans a
     ) a

实际上,这并不完全正确,因为您必须为nexttd和prevtd考虑null。但是,这个想法很简单。lead和lag功能允许您获取prev或text事务,然后您可以执行任何您想要找到最小值的操作,或者从记录中获取任何您想要的其他信息。

谢谢!如果我也想知道查询结果的日期,你会怎么做?我一直在尝试,但只是得到了一个含糊不清的列定义错误。我在用甲骨文,谢谢!如果我也想知道查询结果的日期,你会怎么做?我一直在尝试,但只是得到了一个含糊不清的列定义错误。我正在使用Oracle。