SQL语句

1.获取不同设备的最后一条记录 SQL消费表中查找所有用户最后一条消费记录

 select a.* from GPRS.DBO.View_GPRS

a where not exists(select 1 from GPRS.DBO.View_GPRS where 设备号 = a.设备号 and 采集时间 > a.采集时间 )

优化:

select a.* from [TYN].[dbo].[View_SolarEnergy] a
where not exists(select 1 from [TYN].[dbo].[View_SolarEnergy] b
where b.设备编号 = a.设备编号 and b.数据采集时间 > a.数据采集时间)

2.获取当天时间最新的20条数据,并且各个字段值都不为空

string cmd = "select TOP 20 温度,电池电压,电池电量,采集时间 from View_GPRS where 温度 is not null and 电池电压 is not null and 电池电量 is not null and 采集时间>'" + currentDateTime + "' and 设备号 ='" + IMEI + "' order by 采集时间 desc";

select TOP 20 温度,电池电压,电池电量,采集时间 from GPRS.dbo.View_GPRS
where 温度 is not null and 电池电压 is not null and 电池电量 is not null and 采集时间>'2019/10/17' and 设备号 ='868221048182987' order by 采集时间 desc

原文地址:https://www.cnblogs.com/youzi-xuchongyou/p/11686897.html