主要实现查询天气数据.
数据如下:
TAGTIME(接受时间) TEMPERATURECODE(温度)
2011-04-11 14:34:29 17.5
2011-04-11 14:34:26 17.5
2011-04-11 14:34:23 17.5
2011-04-11 14:34:22 17.5
2011-04-11 14:34:03 18.00
2011-04-11 14:34:01 18.00
2011-04-11 14:34:00 17.5
2011-04-11 14:33:47 17.5
2011-04-11 14:34:02 16.5
2011-04-11 14:32:47 16.5
查询结果要求:一段时间内只显示变化的温度,查询出最早的时间点
结果如下:
2011-04-11 14:34:22 17.5
2011-04-11 14:34:01 18.00
2011-04-11 14:33:47 17.5
2011-04-11 14:32:47 16.5
数据如下:
TAGTIME(接受时间) TEMPERATURECODE(温度)
2011-04-11 14:34:29 17.5
2011-04-11 14:34:26 17.5
2011-04-11 14:34:23 17.5
2011-04-11 14:34:22 17.5
2011-04-11 14:34:03 18.00
2011-04-11 14:34:01 18.00
2011-04-11 14:34:00 17.5
2011-04-11 14:33:47 17.5
2011-04-11 14:34:02 16.5
2011-04-11 14:32:47 16.5
查询结果要求:一段时间内只显示变化的温度,查询出最早的时间点
结果如下:
2011-04-11 14:34:22 17.5
2011-04-11 14:34:01 18.00
2011-04-11 14:33:47 17.5
2011-04-11 14:32:47 16.5
select * from tb
where tagtime=(select min(tagtime) from tb)
and tagtime between ... and ...
insert into test values(to_date('2011-04-11 14:34:26','yyyy-mm-dd hh24:mi:ss'),'17.5');
insert into test values(to_date('2011-04-11 14:34:23','yyyy-mm-dd hh24:mi:ss'),'17.5');
insert into test values(to_date('2011-04-11 14:34:22','yyyy-mm-dd hh24:mi:ss'),'17.5');
insert into test values(to_date('2011-04-11 14:34:03','yyyy-mm-dd hh24:mi:ss'),'18.00');
insert into test values(to_date('2011-04-11 14:34:01','yyyy-mm-dd hh24:mi:ss'),'18.00');
insert into test values(to_date('2011-04-11 14:34:00','yyyy-mm-dd hh24:mi:ss'),'17.5');
insert into test values(to_date('2011-04-11 14:33:47','yyyy-mm-dd hh24:mi:ss'),'17.5');
insert into test values(to_date('2011-04-11 14:34:02','yyyy-mm-dd hh24:mi:ss'),'16.5');
insert into test values(to_date('2011-04-11 14:32:47','yyyy-mm-dd hh24:mi:ss'),'16.5');
select a.* from
( select rownum anum,t.* from test t order by t.tagtime desc )a
where not exists(select * from (select rownum bnum,t.* from test t order by t.tagtime desc) b where b.bnum=a.anum+1 and b.temperaturecode=a.temperaturecode)
order by a.anum asc
(select TAGTIME,
(case (select top 1 TEMPERATURECODE from test b where b.TAGTIME<a.TAGTIME) when a.TEMPERATURECODE then 0 else a.TEMPERATURECODE end ) as TEMPERATURECODE
from test a) t
where t.TEMPERATURECODE<>0
CREATE TABLE [dbo].[DEMO](
[TAGTIME] [varchar](50) NULL,
[temperature] [varchar](50) NULL,
[Code] [varchar](50) NULL
) ON [PRIMARY]GOSET ANSI_PADDING OFF
GOSELECT * FROM demo t WHERE NOT EXISTS
(SELECT * FROM demo WHERE Code=t.Code AND temperature>t.temperature)
select TAGTIME,TEMPERATURECODE
from tb t
where not exists (select 1 from tb where TEMPERATURECODE
=t.TEMPERATURECODE and TAGTIME > t.TAGTIME
)
insert into test values('2011-04-11 14:34:29',17.5,null);
insert into test values('2011-04-11 14:34:26',17.5,null);
insert into test values('2011-04-11 14:34:23',17.5,null);
insert into test values('2011-04-11 14:34:22',17.5,null);
insert into test values('2011-04-11 14:34:03',18.00,null);
insert into test values('2011-04-11 14:34:01',18.00,null);
insert into test values('2011-04-11 14:34:00',17.5,null);
insert into test values('2011-04-11 14:33:47',17.5,null);
insert into test values('2011-04-11 14:34:02',16.5,null);
insert into test values('2011-04-11 14:32:47',16.5,null);
declare @num int,@num2 numeric(18,1)
select @num=0,@num2=0
update a set no=@num,@num=(case when @num2=num then @num else @num+1 end),@num2=a.num
from test aselect [time],num from test a
where not exists(select 1 from test where num=a.num and no=a.no and a.[time]<[time])/*
time num
----------------------- ---------------------------------------
2011-04-11 14:34:29.000 17.5
2011-04-11 14:34:03.000 18.0
2011-04-11 14:34:00.000 17.5
2011-04-11 14:34:02.000 16.5
insert into test values('2011-04-11 14:34:29',17.5,null);
insert into test values('2011-04-11 14:34:26',17.5,null);
insert into test values('2011-04-11 14:34:23',17.5,null);
insert into test values('2011-04-11 14:34:22',17.5,null);
insert into test values('2011-04-11 14:34:03',18.00,null);
insert into test values('2011-04-11 14:34:01',18.00,null);
insert into test values('2011-04-11 14:34:00',17.5,null);
insert into test values('2011-04-11 14:33:47',17.5,null);
insert into test values('2011-04-11 14:34:02',16.5,null);
insert into test values('2011-04-11 14:32:47',16.5,null);
declare @num int,@num2 numeric(18,1)
select @num=0,@num2=0
update a set no=@num,@num=(case when @num2=num then @num else @num+1 end),@num2=a.num
from test aselect [time],num from test a
where not exists(select 1 from test where num=a.num and no=a.no and a.[time]>[time])/*
time num
----------------------- ---------------------------------------
2011-04-11 14:34:22.000 17.5
2011-04-11 14:34:01.000 18.0
2011-04-11 14:33:47.000 17.5
2011-04-11 14:32:47.000 16.5
insert into tb values('2011-04-11 14:34:29',17.5)
insert into tb values('2011-04-11 14:34:26',17.5)
insert into tb values('2011-04-11 14:34:23',17.5)
insert into tb values('2011-04-11 14:34:22',17.5)
insert into tb values('2011-04-11 14:34:03',18.0)
insert into tb values('2011-04-11 14:34:01',18.0)
insert into tb values('2011-04-11 14:34:00',17.5)
insert into tb values('2011-04-11 14:33:47',17.5)
insert into tb values('2011-04-11 14:33:02',16.5) --楼主提供的数据这儿错了,不应为34分,否则得不到楼主的结果
insert into tb values('2011-04-11 14:32:47',16.5)
go
select *
from tb a
where not exists(
select 1 from tb b where [time]>a.[time] and num=a.num and not exists(
select 1 from tb where [time]>a.[time] and [time]<b.[time] and num!=a.num
)
)
go
drop table tb
/*
time num
----------------------- ---------------------------------------
2011-04-11 14:34:29.000 17.5
2011-04-11 14:34:03.000 18.0
2011-04-11 14:34:00.000 17.5
2011-04-11 14:33:02.000 16.5(4 行受影响)
*/
create table tb([time] datetime,num numeric(18,1))
insert into tb values('2011-04-11 14:34:29',17.5)
insert into tb values('2011-04-11 14:34:26',17.5)
insert into tb values('2011-04-11 14:34:23',17.5)
insert into tb values('2011-04-11 14:34:22',17.5)
insert into tb values('2011-04-11 14:34:03',18.0)
insert into tb values('2011-04-11 14:34:01',18.0)
insert into tb values('2011-04-11 14:34:00',17.5)
insert into tb values('2011-04-11 14:33:47',17.5)
insert into tb values('2011-04-11 14:33:02',16.5) --楼主提供的数据这儿错了,不应为34分,否则得不到楼主的结果
insert into tb values('2011-04-11 14:32:47',16.5)
go
select *
from tb a
where not exists(
select 1 from tb b where [time]<a.[time] and num=a.num and not exists(
select 1 from tb where [time]<a.[time] and [time]>b.[time] and num!=a.num
)
)
go
drop table tb
/*
time num
----------------------- ---------------------------------------
2011-04-11 14:34:22.000 17.5
2011-04-11 14:34:01.000 18.0
2011-04-11 14:33:47.000 17.5
2011-04-11 14:32:47.000 16.5(4 行受影响)*/