34,873
社区成员
发帖
与我相关
我的任务
分享--> 测试数据:[tb]
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO
CREATE TABLE [tb]([时间] DATETIME,[设备1] VARCHAR(7),[设备2] INT)
INSERT [tb]
SELECT '2012-9-30 20:00',100,420 UNION ALL
SELECT '2012-9-30 21:00',110,430 UNION ALL
SELECT '2012-9-30 22:00',120,440 UNION ALL
SELECT '2012-9-30 23:00',142,445 UNION ALL
SELECT '2012-10-1 00:00',210,449 UNION ALL
SELECT '2012-10-1 1:00:00',200,500 UNION ALL
SELECT '2012-10-1 2:00:00',300,200 UNION ALL
SELECT '2012-10-1 3:00:00',345,550 UNION ALL
SELECT '2012-10-1 4:00:00',352,0 UNION ALL
SELECT '2012-10-1 5:00:00',160,610
--------------开始查询--------------------------
SELECT [时间],CASE WHEN [设备1]<(SELECT [设备1] FROM [tb] WHERE [时间]=DATEADD(hh,-1,t.[时间]))
THEN (SELECT [设备1] FROM [tb] WHERE [时间]=DATEADD(hh,-1,t.[时间]))
ELSE [设备1] END
,CASE WHEN [设备2]<(SELECT [设备2] FROM [tb] WHERE [时间]=DATEADD(hh,-1,t.[时间]))
THEN (SELECT [设备2] FROM [tb] WHERE [时间]=DATEADD(hh,-1,t.[时间]))
ELSE [设备2] END
FROM [tb] AS t
----------------结果----------------------------
/*
时间 (无列名) (无列名)
2012-09-30 20:00:00.000 100 420
2012-09-30 21:00:00.000 110 430
2012-09-30 22:00:00.000 120 440
2012-09-30 23:00:00.000 142 445
2012-10-01 00:00:00.000 210 449
2012-10-01 01:00:00.000 210 500
2012-10-01 02:00:00.000 300 500
2012-10-01 03:00:00.000 345 550
2012-10-01 04:00:00.000 352 550
2012-10-01 05:00:00.000 352 610
*/

create trigger tri_insert
on tb
instead of insert
as
begin
insert into tb
select tb.时间,
tb.设备1 = case when 设备1>i.设备1 then 设备1 else i.设备1 end,
tb.设备2 = case when 设备2>i.设备2 then 设备2 else i.设备2 end,
tb.设备3 = case when 设备3>i.设备3 then 设备3 else i.设备3 end,
tb.设备4 = case when 设备4>i.设备4 then 设备4 else i.设备4 end
from tb join inserted i on tb.时间=i.时间-1
end/*
create table tb(时间 datetime,设备1 varchar(10), 设备2 varchar(10), 设备3 varchar(10), 设备4 varchar(10))
insert into tb
select '2012-9-1','10000','15000','12000','23000' union all
select '2012-9-2','12000','16000','12300','24500'
*/
-- select * from tb
create procedure pp
(@dt datetime,@mach1 varchar(10), @mach2 varchar(10), @mach3 varchar(10), @mach4 varchar(10))
as
insert into tb
select
@dt,
设备1 = case when @mach1>a.设备1 then @mach1 else a.设备1 end,
设备2 = case when @mach2>a.设备2 then @mach2 else a.设备2 end,
设备3 = case when @mach3>a.设备3 then @mach3 else a.设备3 end,
设备4 = case when @mach4>a.设备4 then @mach4 else a.设备4 end
from (select top 1 时间,设备1,设备2,设备3,设备4 from tb order by 时间 desc)as a
EXEC pp @dt='2012-9-3',
@mach1='0',
@mach2='130',
@mach3='12350',
@mach4='25300'
-- drop proc pp

if OBJECT_ID('tab') is not null
drop table tab
if OBJECT_ID('t') is not null
drop trigger t
go
create table tab(d_date date,device1 int,device2 int,device3 int,device4 int)
insert into tab
select '2012-9-1', 10000, 15000, 12000, 23000 union
select '2012-9-2', 12000, 16000, 12300, 24500 union
select '2012-9-3', 12000, 16000, 12300, 24500
go
create trigger t
on tab
instead of insert
as begin
declare @dev1 int,@dev2 int,@dev3 int,@dev4 int,@idev1 int,@idev2 int,@idev3 int,@idev4 int,@date date
select @dev1=b.device1 from tab as b where not exists(select 1 from tab where device1>b.device1 )
select @dev2=b.device2 from tab as b where not exists(select 1 from tab where device1>b.device1 )
select @dev3=b.device3 from tab as b where not exists(select 1 from tab where device1>b.device1 )
select @dev4=b.device4 from tab as b where not exists(select 1 from tab where device1>b.device1 )
select @idev1=device1 from inserted
select @idev2=device2 from inserted
select @idev3=device3 from inserted
select @idev4=device4 from inserted
select @date=d_date from inserted
if (@idev1>=@dev1 and @idev2>=@dev2 and @idev3>=@dev3 and @idev4>=@dev4)
insert into tab select * from inserted
else insert into tab(d_date,device1,device2,device3,device4)
values(@date,@dev1,@dev2,@dev3,@dev4)
end
go
insert into tab
select '2012-9-4', 12000, 15000, 12300, 24500
select * from tab
/*
(3 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
d_date device1 device2 device3 device4
---------- ----------- ----------- ----------- -----------
2012-09-01 10000 15000 12000 23000
2012-09-02 12000 16000 12300 24500
2012-09-03 12000 16000 12300 24500
2012-09-04 12000 16000 12300 24500
(4 row(s) affected)
*/