我有一张桌子,
<p>This one is with flex-grow</p>
<div class="flex">
<div class="col1">1 col</div>
<div class="col2 fg">2 col</div>
</div>
<p>This one is with width 100%</p>
<div class="flex">
<div class="col1">1 col</div>
<div class="col2 fw">2 col</div>
</div>
现在,我想获得在同一datetime(2014-08-29 08:44:34.122)上创建的Ticket_NUM,其最大时差小于60秒。谁能让我知道如何编写查询以获取此数据。我在桌子上使用了自我联接,但没有得到想要的Ticket_NUM(2,5,3)。
答案 0 :(得分:0)
一种方法是使用LEAD
和LAG
select * from(
select
*,
myFlag = case
when abs(datediff(second,[date],lag([Date]) over (order by [Date]))) < 60 then 1
when abs(datediff(second,[date],lead([Date]) over (order by [Date]))) < 60 then 1
else 0
end
from yourtable)
where myFlag = 1
order by [Date]
您可以删除where myFlag = 1
来查看标记了哪些。
或更丑陋的方法:
with cte as(
select *, RN = row_number() over (order by [Date])
from yourtable
),
staging as(
select
c.ID
,c.Ticket_NUM
,c.[Date]
,c.Comments
,c2DT = min(c2.[Date])
,c3DT = max(c3.[Date])
from
cte c
left join cte c2 on c2.RN = c.RN + 1 --row above
left join cte c3 on c3.RN = c.RN - 1 --row below
group by
c.ID
,c.Ticket_NUM
,c.[Date]
,c.Comments)
select
c.ID
,c.Ticket_NUM
,c.[Date]
,c.Comments
from stating c
where abs(datediff(second,c.[Date],c.c2DT)) < 60 or abs(datediff(second,c.[Date],c.c3DT)) < 60