计算连续行和筛选器SQL Server之间的更改值

计算连续行和筛选器SQL Server之间的更改值,sql,sql-server,Sql,Sql Server,我必须创建一个跟踪某些设备版本的表。该表用于跟踪任何版本升级以及设备在一天中的不同时间ping其版本。最近,我注意到,如果一个用户使用/注册另一台设备,那么对于同一用户,该表将在同一天有多个版本ping。不幸的是,我无法更改此表的基础架构或将版本ping收集到不同表集中的逻辑 现在的问题是,当其中一台设备发生版本升级时,该表将显示给定日期的两个不同版本,这是正确的,但是由于用户有另一台设备,我也会得到该设备的版本 我的目标是过滤掉任何超过1台设备的id。现在在下面的示例中,让我们假设9.X是an

我必须创建一个跟踪某些设备版本的表。该表用于跟踪任何版本升级以及设备在一天中的不同时间ping其版本。最近,我注意到,如果一个用户使用/注册另一台设备,那么对于同一用户,该表将在同一天有多个版本ping。不幸的是,我无法更改此表的基础架构或将版本ping收集到不同表集中的逻辑

现在的问题是,当其中一台设备发生版本升级时,该表将显示给定日期的两个不同版本,这是正确的,但是由于用户有另一台设备,我也会得到该设备的版本

我的目标是过滤掉任何超过1台设备的id。现在在下面的示例中,让我们假设9.X是android设备,所有1.X版本都是ios。在这种情况下,id 123有两台设备,我需要从数据集中删除123以便进一步分析。我需要跟踪合法升级,因此在下面的案例中,只有234和345是使用1台设备的合法升级。尽管123的第一台设备将ios从1.6升级到1.7,但我无法将其包括在分析中,因为123 id有另一台使用安卓9.0的设备

123    1.7    2017-08-11 22:57:54.307
123    9.0    2017-08-11 21:37:54.307 <-- 123 second device version
123    1.7    2017-08-11 20:27:54.307
123    1.7    2017-08-11 19:17:54.307 <-- 123 first device version upgrade
123    1.6    2017-08-11 18:47:54.307
123    1.6    2017-08-11 17:37:54.307
123    9.0    2017-08-11 16:57:54.307 <-- 123 signed up with a new device
123    1.6    2017-08-11 15:17:54.307 <-- 123 first device version

234    1.7    2017-08-11 22:47:54.307
234    1.7    2017-08-11 21:47:54.307 <-- 234 first device version upgrade
234    1.5    2017-08-11 20:47:54.307
234    1.5    2017-08-11 19:47:54.307
234    1.5    2017-08-11 18:47:54.307 <-- 234 first device version

345    1.8    2017-08-11 22:47:54.307
345    1.8    2017-08-11 21:47:54.307 <-- 345 first device version upgrade
345    1.4    2017-08-11 20:47:54.307
345    1.4    2017-08-11 19:47:54.307
345    1.4    2017-08-11 18:47:54.307 <-- 345 first device version
电流输出-我需要在第一步过滤掉123

123    
234    
345    
预期输出-我的脚本适用于所有具有单个设备和合法升级的ID

234    
345    

如果我理解你的问题,也许这会简化一点

示例

Select Top 1 With Ties *
 From  YourTable
 Where ID not in (Select ID From YourTable Group By ID having floor(min(ver))<>floor(max(ver)))
 Order By Row_Number() over (Partition by id order by dt_create desc)
编辑-按ID、日期划分

Select Top 1 With Ties *
 From  YourTable
 Where ID not in (Select ID From YourTable Group By ID having floor(min(ver))<>floor(max(ver)))
 Order By Row_Number() over (Partition by id order by dt_create desc)
使用LEAD()

1)

2)

4)

5)


对于给定的一天,如果有两个以上的版本,我可以假设id有另一个设备,即1进行了升级,然后有另一个。我可以确定的第二种方法是,对于给定的一天,我得到多个版本ping,如x,y,x,y,x,y。第二种方法的问题是,每当设备进行升级时,我肯定会有一个x,y版本组合。我只需要在第一步筛选出具有多个设备的用户。谢谢,如果我计算每个ids版本ping不同的次数,我可能会得到123个筛选。就像上面的情况一样,在给定的一天内,id 123的连续行之间有5个版本组合。1.6-9.0,9.0-1.6,1.6-1.7,1.7-9.0,最后是9.0-1.7。可能是版本滞后/超前的组合?@好奇的心也许编辑更接近你需要的。你认为我下面的解决方案如何?做得好!抱歉,我被抽离+1谢谢,与您进行讨论帮助我找到了解决方案。
id   ver    dt_create
234  1.7    2017-08-11 22:47:54.307
345  1.8    2017-08-11 22:47:54.307
;with cte as (
Select *
      ,VerCnt = sum(1)   over (partition by id,cast(dt_create as date))
      ,VerMin = min(ver) over (partition by id,cast(dt_create as date))
      ,VerMax = max(ver) over (partition by id,cast(dt_create as date))
 From  @YourTable 
)
Select top 1 with ties
       id,ver,dt_create
 From  cte
 Where floor(VerMin)=floor(VerMax)
   and VerCnt>1
 Order By Row_Number() over (Partition by id order by dt_create desc)
select id,ver,lead(id,1) over (order by id,dt_create desc) as next_id,lead(ver,1) over (order by id,dt_create desc ) as next_ver,dt_create
into #temp1
from Z_version
order by id,dt_create desc

id  ver next_id next_ver    dt_create
123 1.7 123 9.0 2017-08-11 22:57:54.307
123 9.0 123 1.7 2017-08-11 21:37:54.307
123 1.7 123 1.7 2017-08-11 20:27:54.307
123 1.7 123 1.6 2017-08-11 19:17:54.307
123 1.6 123 1.6 2017-08-11 18:47:54.307
123 1.6 123 9.0 2017-08-11 17:37:54.307
123 9.0 123 1.6 2017-08-11 16:57:54.307
123 1.6 123 1.6 2017-08-11 15:17:54.307
123 1.6 123 1.6 2017-07-11 22:57:54.307
123 1.6 123 1.6 2017-07-11 21:37:54.307
123 1.6 123 1.6 2017-07-11 20:27:54.307
123 1.6 234 1.7 2017-07-11 19:17:54.307
234 1.7 234 1.7 2017-08-11 22:47:54.307
234 1.7 234 1.5 2017-08-11 22:47:54.307
234 1.5 234 1.5 2017-08-11 20:47:54.307
234 1.5 234 1.5 2017-08-11 19:47:54.307
234 1.5 234 1.5 2017-08-11 18:47:54.307
234 1.5 234 1.3 2017-07-11 22:47:54.307
234 1.3 234 1.3 2017-07-11 20:47:54.307
234 1.3 234 1.3 2017-07-11 19:47:54.307
234 1.3 345 1.8 2017-07-11 18:47:54.307
345 1.8 345 1.8 2017-08-11 22:47:54.307
345 1.8 345 1.4 2017-08-11 21:47:54.307
345 1.4 345 1.4 2017-08-11 20:47:54.307
345 1.4 345 1.4 2017-08-11 19:47:54.307
345 1.4 345 1.4 2017-08-11 18:47:54.307
345 1.4 NULL    NULL    2017-07-11 06:34:35.307
select * into #temp2 from #temp1 where ver<>next_ver and id=next_id

id  ver next_id next_ver    dt_create
123 1.7 123 9.0 2017-08-11 22:57:54.307
123 9.0 123 1.7 2017-08-11 21:37:54.307
123 1.7 123 1.6 2017-08-11 19:17:54.307
123 1.6 123 9.0 2017-08-11 17:37:54.307
123 9.0 123 1.6 2017-08-11 16:57:54.307
234 1.7 234 1.5 2017-08-11 22:47:54.307
234 1.5 234 1.3 2017-07-11 22:47:54.307
345 1.8 345 1.4 2017-08-11 21:47:54.307
select id,convert(varchar(12),dt_create,112) as each_day,count(ver) as ver_count into #temp3 from #temp2
group by id,convert(varchar(12),dt_create,112) order by id

id  each_day    ver_count
123 20170811    5
234 20170711    1
234 20170811    1
345 20170811    1
select id,max(ver_count) max_count into #temp4 from #temp3 group by id

id  max_count
123 5
234 1
345 1
select id,max_count from #temp4 where max_count < 2 

id  max_count
234 1
345 1
drop table #temp1
drop table #temp2
drop table #temp3
drop table #temp4