SELECT DeviceId, CAST(ROUND(AVG(distance / time_diff),5,1) as decimal(18,5)) AS '移动速率' FROM ( SELECT DeviceId, DATEDIFF(second, LAG(StartTime) OVER (PARTITION BY DeviceId ORDER BY StartTime), StartTime) AS time_diff, SQRT(POWER(V1 - LAG(V1) OVER (PARTITION BY DeviceId ORDER BY StartTime), 2) + POWER(V2 - LAG(V2) OVER (PARTITION BY DeviceId ORDER BY StartTime), 2) + POWER(V3 - LAG(V3) OVER (PARTITION BY DeviceId ORDER BY StartTime), 2)) AS distance FROM T_BMWY_ResolveDeviceData WHERE StartTime >= DATEADD(day, -5, GETDATE()) ) t WHERE time_diff > 0 GROUP BY DeviceId 把这个语句改成sqlserver2008 版本的语句

时间: 2024-03-23 13:40:24 浏览: 11
在 SQL Server 2008 中,不支持 LAG 和 DATEDIFF 函数,需要使用其他方法来编写相同的查询。以下是一种可能的替代方法: ``` SELECT DeviceId, CAST(ROUND(AVG(distance / time_diff), 5, 1) AS decimal(18,5)) AS '移动速率' FROM ( SELECT t1.DeviceId, t1.StartTime, t1.V1, t1.V2, t1.V3, DATEDIFF(SECOND, t2.StartTime, t1.StartTime) AS time_diff, SQRT(POWER(t1.V1 - t2.V1, 2) + POWER(t1.V2 - t2.V2, 2) + POWER(t1.V3 - t2.V3, 2)) AS distance FROM T_BMWY_ResolveDeviceData t1 JOIN T_BMWY_ResolveDeviceData t2 ON t1.DeviceId = t2.DeviceId AND t1.StartTime > t2.StartTime WHERE t1.StartTime >= DATEADD(DAY, -5, GETDATE()) ) t WHERE time_diff > 0 GROUP BY DeviceId ``` 上述查询使用了自连接和 WHERE 语句来模拟 LAG 函数。它还使用了 POWER 函数来计算距离,而不是 SQRT 函数中的平方根。如果需要,可以根据具体情况进行调整。

相关推荐

select concat(radio_mcc,Radio_MNC) as plmn,Radio_Brand,Radio_Network_Generation,left(Test_Timestamp,7) as month,traffic_bytes,traffic_direction,Location_Longitude as longtition,Location_Latitude as latitude cast((180.0 * (cast(floor(cast(Location_Longitude as double) * 20037508.34 / 180.0 / 300) as int) * 300 + 50) / 20037508.34) as decimal(30,10)) as grid_longitude, case when ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() >1 then cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(20037508.34/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) when ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() < -1 then cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(-20037508.34/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) else cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() * 20037508/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) end as grid_latitude, avg(throughput_receive) as download_bps, sum(test_samples) as samples from public_standard.ps04_p3_ntr__no where Radio_Network_Generation not in ('WiFi','Unknown','2G') and concat(radio_mcc,Radio_MNC)!='' and concat(radio_mcc,Radio_MNC) in (select a.plmn from (select concat(radio_mcc,Radio_MNC) as plmn,count(0) as num from public_standard.ps04_p3_ntr__no where concat(radio_mcc,Radio_MNC)!='' group by concat(radio_mcc,Radio_MNC) order by num desc limit 4)a ) group by radio_mcc,Radio_MNC,Radio_Brand,Radio_Network_Generation,month,traffic_bytes,traffic_direction,grid_longitude,grid_latitude,Location_Longitude,Location_Latitude;改为正确写法

请以最详细的方式解释这段代码with a01 as( select sm_id, isname, case when isname in ('冰箱', '冷柜') then '制冷' when isname = '商空' then '楼宇' when isname = '家空' then '空调' else isname end as cyx, factory_code, mtname, curstatus, model_code, model_name, start_time, --'保养起始时间' expire_time, --'保养到期时间' substring(start_time, 1, 7) byqsny, substring(expire_time, 1, 7) byjssj, case when stype = '2' then '二保' when stype = '3' then '三保' else stype end as bylx, case when mm_type = '2' then '二保' when mm_type = '3' then '三保' when mm_type = '4' then '三保带二保' else mm_type end as mmtype, --实际保养类型 upper_time, --上次保养时间 mm_time, --实际保养时间 scustcode, -- 实供应商编码 scustname, --实供应商名称 mould_status, --模具维保状态 mm_status, --实际是否保养 smould_status --模具实际维保状态 from dwd_mm_staymaintain_all a where by_status <> '无需保养' and length(factory_code) = 4 and curstatus not in ('报废中', '已报废', '冻结数据', '垃圾数据') and isname in ('冰箱', '冷柜', '家空', '商空', '洗涤', '厨电', '热水器') and substring(expire_time, 1, 10) <= from_unixtime(unix_timestamp(), 'yyyy-MM-dd') and expire_time is not null and expire_time <> '' ), b01 as( select cyx, byjssj, count(sm_id) by_ying from a01 group by cyx, byjssj ), b02 as( select cyx, byjssj, count(sm_id) by_shi from a01 where mm_status = '已保养' group by cyx, byjssj ),c01 as( select from_unixtime(unix_timestamp(), 'yyyy-MM') months, t1.cyx industry, t1.byjssj byjsyf, cast(t1.by_ying as string) by_ying, cast(t2.by_shi as string) by_shi, cast(t2.by_shi/t1.by_ying as decimal(8,5)) bywcl from b01 t1 left join b02 t2 on t1.cyx = t2.cyx and t1.byjssj = t2.byjssj) insert overwrite table dh_yf.tt_mould_asset_maintenance_overview select months, industry, byjsyf, by_ying, by_shi, cast(bywcl as string) bywcl, from_unixtime(unix_timestamp(), 'yyyy-MM-dd') etl_date, '%' unit from c01

create table $x00256702.$pr01_temp_p3_ntr_grid_new__1604666481550 as select concat(radio_mcc,Radio_MNC) as plmn,Radio_Brand,Radio_Network_Generation, left(Test_Timestamp,7) as month, cast((180.0 * (cast(floor(cast(Location_Longitude as double) * 20037508.34 / 180.0 / 300) as int) * 300 + 50) / 20037508.34) as decimal(30,10)) as grid_longitude, case when ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() >1 then cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(20037508.34/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) when ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() < -1 then cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(-20037508.34/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) else cast((57.295779513082323 * (2.0 * atan(exp((cast(floor(ln(tan((90.0 +cast(Location_Latitude as double)) * pi() / 360.0)) / pi() * 20037508/300) as int)*300 + 50) /20037508.34 * 3.141592653589793)) - 1.570796326794897)) as decimal(30,10)) end as grid_latitude, avg(throughput_receive) as download_bps,sum(test_samples) as samples from $public_standard.$ps04_p3_ntr__za where Radio_RXLevel>=-150 and Radio_RXLevel<=-10 and Radio_Network_Generation!='WiFi' and traffic_direction='Downlink' and concat(radio_mcc,Radio_MNC)!='' and concat(radio_mcc,Radio_MNC) in (select a.plmn from (select concat(radio_mcc,Radio_MNC) as plmn,count(0) as num from $public_standard.$ps04_p3_ntr__za where concat(radio_mcc,Radio_MNC)!='' group by concat(radio_mcc,Radio_MNC) order by num desc limit 4)a ) and left(test_timestamp,7) in (left(cast(months_add(now(),0) as string),7), left(cast(months_add(now(),-1) as string),7), left(cast(months_add(now(),-2) as string),7), left(cast(months_add(now(),-3) as string),7), left(cast(months_add(now(),-4) as string),7), left(cast(months_add(now(),-5) as string),7), left(cast(months_add(now(),-6) as string),7)) group by radio_mcc,Radio_MNC,Radio_Brand,Radio_Network_Generation,month,grid_longitude,grid_latitude ;解析一下这个算法脚本

最新推荐

recommend-type

Python decimal模块使用方法详解

主要介绍了Python decimal模块使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

C#中decimal保留2位有效小数的实现方法

主要介绍了C#中decimal保留2位有效小数的实现方法,针对decimal变量保留2位有效小数有多种方法,可以使用Math.Round方法以及ToString先转换为字符串等操作来实现。具体实现方法感兴趣的朋友跟随小编一起看看吧
recommend-type

MySQL数据类型中DECIMAL的用法实例详解

2 3 4 5 6,则结果为1. 2。如果将相同的值存入FLOAT(8, 4) 的列中,则结果为1. 2 3 4 6。 这表示应该定义具有足够位数的浮点列以便得到尽可能精确的值。如果想精确到千分之一,那就不要定义使该类型仅有两位小数。 ...
recommend-type

详解MySQL数据类型DECIMAL(N,M)中N和M分别表示的含义

关于MySQL数据类型decimal中n和m分别表示什么含义?本文就此问题作了简单论述,并创建相关表进行验证,需要的朋友可以了解下。
recommend-type

基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码(高分优秀项目)

基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于C/C++开发的单目控制机械臂的上位机程序+视觉识别和关节角反解+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。