在你创建表的时候把code设成自动增长 例如: create table t1 ( code int identity(10000,1) not null, names varchar(20) null, idate datetime null )
研究出一个更新语句,可以一试: update t1 set code=CAST(10000+ b.id as varchar) from t1 t join ( select *,ROW_NUMBER() over (order by indate) as id from t1 ) as b on t.code=b.code and t.indate=b.indate and t.names=b.names
MS-SQL 去持 ROW_NUMBER吗?
支持的/* 系统不断的在读取每台正常运行的设备的某些参数 放到如下的表中:机器编号,参数1,参数2,时间 1 0.15 0.22 **** 2 0.14 0.23 **** 2 0.15 0.32 **** 1 0.16 0.32 **** 1 0.15 0.22 **** 4 0.13 0.232 **** 1 0.15 0.22 ****如何用一条语句得到截止到某个时间的,每台机器的最后一条记录?----如果同一时同一台机器没有重复记录的话,可以用下面的查询 --测试 declare @t table(机器编号 int,参数1 numeric(10,2),参数2 numeric(10,2),时间 datetime) insert into @t select 1, 0.15 ,0.22, '2013-04-12 08:20:00' union all select 2 , 0.14 ,0.23, '2013-04-12 08:31:00' union all select 2 , 0.15 , 0.32 , '2013-04-12 08:29:00' union all select 1 , 0.16 , 0.32 , '2013-04-12 08:22:00' union all select 1 , 0.15 , 0.22 ,'2013-04-12 08:23:00' union all select 4 , 0.13 , 0.232 ,'2013-04-12 08:30:00' union all select 1 , 0.15, 0.22 ,'2013-04-12 08:27:22'--查询 select x.* from @t x inner join ( select 机器编号,MAX(时间) as 时间 from @t where convert(varchar(10),时间,120)<='2013-04-12 08:30:00' --截止到某个时间,楼主自己设置 group by 机器编号 ) y on x.机器编号=y.机器编号 and x.时间=y.时间--结果 /* 机器编号 参数1 参数2 时间 4 0.13 0.23 2013-04-12 08:30:00.000 2 0.14 0.23 2013-04-12 08:31:00.000 1 0.15 0.22 2013-04-12 08:27:22.000 */ */--如果同一时间同一台机器有重复记录的话,则用下面查询 --搭建测试环境 IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb]') AND type in (N'U')) DROP TABLE [dbo].tb GOcreate table tb (自增量ID int IDENTITY(1,1) NOT NULL,机器编号 int,参数1 numeric(10,2),参数2 numeric(10,2),时间 datetime) insert into tb select 1, 0.15 ,0.22, '2013-04-12 08:20:00' union all select 2 , 0.14 ,0.23, '2013-04-12 08:31:00' union all select 2 , 0.15 , 0.32 , '2013-04-12 08:29:00' union all select 1 , 0.16 , 0.32 , '2013-04-12 08:20:00' union all select 1 , 0.15 , 0.22 ,'2013-04-12 08:23:00' union all select 1 , 0.16 , 0.32 , '2013-04-12 08:22:00' union all select 1 , 0.88 , 0.99 ,'2013-04-12 08:23:00' union all select 4 , 0.13 , 0.23 ,'2013-04-12 08:30:00' union all select 1 , 0.15, 0.22 ,'2013-04-12 08:22:00'--查询 with t as ( select 机器编号,参数1,参数2,时间,row_number() over(partition by 机器编号,时间 order by 自增量ID desc --ws_hgo 少了desc,楼主要求取最后一笔记录 ) as [rank] from tb ) select 机器编号,参数1,参数2,时间 from t where [rank] = 1--测试结果 /* 机器编号 参数1 参数2 时间 1 0.16 0.32 2013-04-12 08:20:00.000 1 0.15 0.22 2013-04-12 08:22:00.000 1 0.88 0.99 2013-04-12 08:23:00.000 2 0.15 0.32 2013-04-12 08:29:00.000 2 0.14 0.23 2013-04-12 08:31:00.000 4 0.13 0.23 2013-04-12 08:30:00.000 */
在你创建表的时候把code设成自动增长
例如:
create table t1
(
code int identity(10000,1) not null,
names varchar(20) null,
idate datetime null
)
update t1 set code=CAST(10000+ b.id as varchar)
from t1 t join (
select *,ROW_NUMBER() over (order by indate) as id from t1
) as b
on t.code=b.code and t.indate=b.indate and t.names=b.names
支持的/*
系统不断的在读取每台正常运行的设备的某些参数
放到如下的表中:机器编号,参数1,参数2,时间
1 0.15 0.22 ****
2 0.14 0.23 ****
2 0.15 0.32 ****
1 0.16 0.32 ****
1 0.15 0.22 ****
4 0.13 0.232 ****
1 0.15 0.22 ****如何用一条语句得到截止到某个时间的,每台机器的最后一条记录?----如果同一时同一台机器没有重复记录的话,可以用下面的查询
--测试
declare @t table(机器编号 int,参数1 numeric(10,2),参数2 numeric(10,2),时间 datetime)
insert into @t select
1, 0.15 ,0.22, '2013-04-12 08:20:00' union all select
2 , 0.14 ,0.23, '2013-04-12 08:31:00' union all select
2 , 0.15 , 0.32 , '2013-04-12 08:29:00' union all select
1 , 0.16 , 0.32 , '2013-04-12 08:22:00' union all select
1 , 0.15 , 0.22 ,'2013-04-12 08:23:00' union all select
4 , 0.13 , 0.232 ,'2013-04-12 08:30:00' union all select
1 , 0.15, 0.22 ,'2013-04-12 08:27:22'--查询
select x.* from @t x
inner join
( select 机器编号,MAX(时间) as 时间 from @t
where convert(varchar(10),时间,120)<='2013-04-12 08:30:00' --截止到某个时间,楼主自己设置
group by 机器编号
) y on x.机器编号=y.机器编号 and x.时间=y.时间--结果
/*
机器编号 参数1 参数2 时间
4 0.13 0.23 2013-04-12 08:30:00.000
2 0.14 0.23 2013-04-12 08:31:00.000
1 0.15 0.22 2013-04-12 08:27:22.000
*/
*/--如果同一时间同一台机器有重复记录的话,则用下面查询
--搭建测试环境
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb]') AND type in (N'U'))
DROP TABLE [dbo].tb
GOcreate table tb (自增量ID int IDENTITY(1,1) NOT NULL,机器编号 int,参数1 numeric(10,2),参数2 numeric(10,2),时间 datetime)
insert into tb select
1, 0.15 ,0.22, '2013-04-12 08:20:00' union all select
2 , 0.14 ,0.23, '2013-04-12 08:31:00' union all select
2 , 0.15 , 0.32 , '2013-04-12 08:29:00' union all select
1 , 0.16 , 0.32 , '2013-04-12 08:20:00' union all select
1 , 0.15 , 0.22 ,'2013-04-12 08:23:00' union all select
1 , 0.16 , 0.32 , '2013-04-12 08:22:00' union all select
1 , 0.88 , 0.99 ,'2013-04-12 08:23:00' union all select
4 , 0.13 , 0.23 ,'2013-04-12 08:30:00' union all select
1 , 0.15, 0.22 ,'2013-04-12 08:22:00'--查询
with t as
(
select 机器编号,参数1,参数2,时间,row_number() over(partition by 机器编号,时间
order by 自增量ID desc --ws_hgo 少了desc,楼主要求取最后一笔记录
) as [rank]
from tb
)
select 机器编号,参数1,参数2,时间 from t where [rank] = 1--测试结果
/*
机器编号 参数1 参数2 时间
1 0.16 0.32 2013-04-12 08:20:00.000
1 0.15 0.22 2013-04-12 08:22:00.000
1 0.88 0.99 2013-04-12 08:23:00.000
2 0.15 0.32 2013-04-12 08:29:00.000
2 0.14 0.23 2013-04-12 08:31:00.000
4 0.13 0.23 2013-04-12 08:30:00.000
*/