27,579
社区成员
发帖
与我相关
我的任务
分享
-- 机器概况表;
DECLARE @c TABLE ( 机器编号 INT );
INSERT INTO @c
VALUES ( 1 ),
( 2 ),
( 3 ),
( 4 );
-- 机器记录表;
DECLARE @t TABLE
(
机器编号 INT ,
参数1 NUMERIC(10, 2) ,
参数2 NUMERIC(10, 2) ,
时间 DATETIME
)
INSERT INTO @t
VALUES ( 1, 0.15, 0.22, '2013-04-12 08:20:00' ),
( 2, 0.14, 0.23, '2013-04-12 08:31:00' ),
( 2, 0.15, 0.32, '2013-04-12 08:29:00' ),
( 1, 0.16, 0.32, '2013-04-12 08:22:00' ),
( 1, 0.15, 0.22, '2013-04-12 08:23:00' ),
( 4, 0.13, 0.232, '2013-04-12 08:30:00' ),
( 1, 0.15, 0.22, '2013-04-12 08:27:22' );
-- 查询语句;
SELECT tt.*
FROM @c c
CROSS APPLY ( SELECT TOP 1
*
FROM @t t
WHERE c.机器编号 = t.机器编号
ORDER BY 时间 desc
) AS tt;
select *
from tb t
where not exists(select 1 from tb where 机器编号=t.机器编号 and 时间>t.时间)
with r as
(
select 机器编号,参数1,参数2,时间,row_number() over(partition by 机器编号,时间 order by 自增量ID) as [rank]
)
select 机器编号,参数1,参数2,时间 where [rank] = 1