--按某一字段分组取最大(小)值所在行的数据
--2007-10-23于杭州
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
*/--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 5 b5b5b5b5b5
*/--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
b 2 b2b2b2b2
*/--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
a 3 a3:a的第三个值
b 4 b4b4
b 5 b5b5b5b5b5
*/
--2007-10-23于杭州
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
*/--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 5 b5b5b5b5b5
*/--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
b 2 b2b2b2b2
*/--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
a 3 a3:a的第三个值
b 4 b4b4
b 5 b5b5b5b5b5
*/
(select a.* from t_scadaMeas a where nAloge = (select max(nAloge) from t_scadaMeas where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))) t1,
(select a.* from t_scadaMeas a where nAloge = (select min(nAloge) from t_scadaMeas where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))) t2
where t1.iEquimentId = t2.iEquimentId and t1.cMeascode = t2.cMeascode and convert(varchar(13),t1.dtScadaTime,120) = convert(varchar(13),t2.dtScadaTime,120)
/* ============================================================ */
/* Table: t_scadaTMeas */
/* ============================================================ */
create table t_scadaTMeas
(
dtScadaTime datetime not null,
nAloge float null ,
iEquimentId integer not null,
cMeascode character(8) not null,
constraint PK_T_SCADAHOURMEAS primary key (dtScadaTime, iEquimentId, cMeascode)
)
go
insert into t_scadaTMeas values('2007/11/20 01:01:00', 1, 1, 'a1')
insert into t_scadaTMeas values('2007/11/20 01:01:03', 3, 1, 'a1')
insert into t_scadaTMeas values('2007/11/20 01:59:03', 3, 1, 'a1')insert into t_scadaTMeas values('2007/11/20 05:01:00', 3, 1, 'a1')
insert into t_scadaTMeas values('2007/11/20 05:01:03', 2, 1, 'a1')
insert into t_scadaTMeas values('2007/11/20 05:59:03', 1, 1, 'a1')insert into t_scadaTMeas values('2007/11/20 03:11:00', 1, 2, 'a2')
insert into t_scadaTMeas values('2007/11/20 03:21:03', 2, 2, 'a2')
insert into t_scadaTMeas values('2007/11/20 03:59:03', 1, 2, 'a2')insert into t_scadaTMeas values('2007/11/20 23:01:00', 3, 3, 'a3')
insert into t_scadaTMeas values('2007/11/20 23:01:03', 3, 3, 'a3')
insert into t_scadaTMeas values('2007/11/20 23:59:03', 3, 3, 'a3')
=== 查询结果
cMeascode iEquimentId ScadaTime max_val time_at_max min_val time_at_min
a1 1 '2007/11/20 01:00:00' 3 '2007/11/20 01:01:03' 1 '2007/11/20 01:01:00'
a1 1 '2007/11/20 05:00:00' 3 '2007/11/20 05:01:00' 1 '2007/11/20 05:59:03'
a2 2 '2007/11/20 03:00:00' 2 '2007/11/20 03:21:03' 1 '2007/11/20 03:11:00'
a1 1 '2007/11/20 23:00:00' 3 '2007/11/20 23:01:00' 3 '2007/11/20 23:01:00'
再次感谢...希望已经能够将问题表达清楚.
cMeascode iEquimentId ScadaTime max_val time_at_max min_val time_at_min
a1 1 '2007/11/20 01:00:00' 3 '2007/11/20 01:01:03' 1 '2007/11/20 01:01:00'
a1 1 '2007/11/20 05:00:00' 3 '2007/11/20 05:01:00' 1 '2007/11/20 05:59:03'
a2 2 '2007/11/20 03:00:00' 2 '2007/11/20 03:21:03' 1 '2007/11/20 03:11:00'
a3 3 '2007/11/20 23:00:00' 3 '2007/11/20 23:01:00' 3 '2007/11/20 23:01:00'
cMeascode iEquimentId ScadaTime max_val time_at_max min_val time_at_min
a1 1 '2007/11/20 01:00:00' 3 '2007/11/20 01:01:03' 1 '2007/11/20 01:01:00'
a1 1 '2007/11/20 05:00:00' 3 '2007/11/20 05:01:00' 1 '2007/11/20 05:59:03'
a2 2 '2007/11/20 03:00:00' 2 '2007/11/20 03:21:03' 1 '2007/11/20 03:11:00'
a3 3 '2007/11/20 23:00:00' 3 '2007/11/20 23:01:00' 3 '2007/11/20 23:01:00'
insert into tb values('2007/11/20 01:01:00', 1, 1, 'a1')
insert into tb values('2007/11/20 01:01:03', 3, 1, 'a1')
insert into tb values('2007/11/20 01:59:03', 3, 1, 'a1')
insert into tb values('2007/11/20 05:01:00', 3, 1, 'a1')
insert into tb values('2007/11/20 05:01:03', 2, 1, 'a1')
insert into tb values('2007/11/20 05:59:03', 1, 1, 'a1')
insert into tb values('2007/11/20 03:11:00', 1, 2, 'a2')
insert into tb values('2007/11/20 03:21:03', 2, 2, 'a2')
insert into tb values('2007/11/20 03:59:03', 1, 2, 'a2')
insert into tb values('2007/11/20 23:01:00', 3, 3, 'a3')
insert into tb values('2007/11/20 23:01:03', 3, 3, 'a3')
insert into tb values('2007/11/20 23:59:03', 3, 3, 'a3')
goselect t1.iEquimentId,t1.cMeascode,convert(varchar(13),t1.dtScadaTime,120) + ':00:00' ScadaTime,t1.nAloge max_nAloge,t2.nAloge min_nAloge,t1.dtScadaTime from
(
select m1.* from
(
select a.* from tb a where nAloge = (select max(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m1 where dtScadaTime =
(
select min(dtScadaTime) from
(
select a.* from tb a where nAloge = (select max(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m2
where m2.iEquimentId = m1.iEquimentId and m2.cMeascode = m1.cMeascode and convert(varchar(13),m2.dtScadaTime,120) = convert(varchar(13),m1.dtScadaTime,120)
)
) t1,
(
select m1.* from
(
select a.* from tb a where nAloge = (select min(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m1 where dtScadaTime =
(
select min(dtScadaTime) from
(
select a.* from tb a where nAloge = (select min(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m2
where m2.iEquimentId = m1.iEquimentId and m2.cMeascode = m1.cMeascode and convert(varchar(13),m2.dtScadaTime,120) = convert(varchar(13),m1.dtScadaTime,120)
)
) t2
where t1.iEquimentId = t2.iEquimentId and t1.cMeascode = t2.cMeascode and convert(varchar(13),t1.dtScadaTime,120) = convert(varchar(13),t2.dtScadaTime,120)
order by t1.iEquimentId , t1.cMeascode , convert(varchar(13),t1.dtScadaTime,120)drop table tb/*
iEquimentId cMeascode ScadaTime max_nAloge min_nAloge dtScadaTime
----------- --------- ------------------- ----------------------------------------------------- ----------------------------------------------------- -----------------------
1 a1 2007-11-20 01:00:00 3.0 1.0 2007-11-20 01:01:03.000
1 a1 2007-11-20 05:00:00 3.0 1.0 2007-11-20 05:01:00.000
2 a2 2007-11-20 03:00:00 2.0 1.0 2007-11-20 03:21:03.000
3 a3 2007-11-20 23:00:00 3.0 3.0 2007-11-20 23:01:00.000(所影响的行数为 4 行)
*/
create table tb (dtScadaTime datetime not null,nAloge float null ,iEquimentId integer not null,cMeascode character(8) not null,constraint PK_T_SCADAHOURMEAS primary key (dtScadaTime, iEquimentId, cMeascode))
insert into tb values('2007/11/20 01:01:00', 1, 1, 'a1')
insert into tb values('2007/11/20 01:01:03', 3, 1, 'a1')
insert into tb values('2007/11/20 01:59:03', 3, 1, 'a1')
insert into tb values('2007/11/20 05:01:00', 3, 1, 'a1')
insert into tb values('2007/11/20 05:01:03', 2, 1, 'a1')
insert into tb values('2007/11/20 05:59:03', 1, 1, 'a1')
insert into tb values('2007/11/20 03:11:00', 1, 2, 'a2')
insert into tb values('2007/11/20 03:21:03', 2, 2, 'a2')
insert into tb values('2007/11/20 03:59:03', 1, 2, 'a2')
insert into tb values('2007/11/20 23:01:00', 3, 3, 'a3')
insert into tb values('2007/11/20 23:01:03', 3, 3, 'a3')
insert into tb values('2007/11/20 23:59:03', 3, 3, 'a3')
goselect t1.iEquimentId,t1.cMeascode,convert(varchar(13),t1.dtScadaTime,120)+':00:00' ScadaTime,t1.nAloge max_nAloge,t2.nAloge min_nAloge,t1.dtScadaTime from
(
select m1.* from
(
select a.* from tb a where nAloge = (select max(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m1 where dtScadaTime =
(
select min(dtScadaTime) from
(
select a.* from tb a where nAloge = (select max(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m2
where m2.iEquimentId = m1.iEquimentId and m2.cMeascode = m1.cMeascode and convert(varchar(13),m2.dtScadaTime,120) = convert(varchar(13),m1.dtScadaTime,120)
)
) t1,
(
select m1.* from
(
select a.* from tb a where nAloge = (select min(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m1 where dtScadaTime =
(
select min(dtScadaTime) from
(
select a.* from tb a where nAloge = (select min(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m2
where m2.iEquimentId = m1.iEquimentId and m2.cMeascode = m1.cMeascode and convert(varchar(13),m2.dtScadaTime,120) = convert(varchar(13),m1.dtScadaTime,120)
)
) t2
where t1.iEquimentId = t2.iEquimentId and t1.cMeascode = t2.cMeascode and convert(varchar(13),t1.dtScadaTime,120) = convert(varchar(13),t2.dtScadaTime,120)
order by t1.iEquimentId , t1.cMeascode , convert(varchar(13),t1.dtScadaTime,120)drop table tb/*
iEquimentId cMeascode ScadaTime max_nAloge min_nAloge dtScadaTime
----------- --------- ------------------- ---------- ---------- -----------------------
1 a1 2007-11-20 01:00:00 3.0 1.0 2007-11-20 01:01:03.000
1 a1 2007-11-20 05:00:00 3.0 1.0 2007-11-20 05:01:00.000
2 a2 2007-11-20 03:00:00 2.0 1.0 2007-11-20 03:21:03.000
3 a3 2007-11-20 23:00:00 3.0 3.0 2007-11-20 23:01:00.000(所影响的行数为 4 行)
*/
create table tb (dtScadaTime datetime not null,nAloge float null ,iEquimentId integer not null,cMeascode character(8) not null,constraint PK_T_SCADAHOURMEAS primary key (dtScadaTime, iEquimentId, cMeascode))
insert into tb values('2007/11/20 01:01:00', 1, 1, 'a1')
insert into tb values('2007/11/20 01:01:03', 3, 1, 'a1')
insert into tb values('2007/11/20 01:59:03', 3, 1, 'a1')
insert into tb values('2007/11/20 05:01:00', 3, 1, 'a1')
insert into tb values('2007/11/20 05:01:03', 2, 1, 'a1')
insert into tb values('2007/11/20 05:59:03', 1, 1, 'a1')
insert into tb values('2007/11/20 03:11:00', 1, 2, 'a2')
insert into tb values('2007/11/20 03:21:03', 2, 2, 'a2')
insert into tb values('2007/11/20 03:59:03', 1, 2, 'a2')
insert into tb values('2007/11/20 23:01:00', 3, 3, 'a3')
insert into tb values('2007/11/20 23:01:03', 3, 3, 'a3')
insert into tb values('2007/11/20 23:59:03', 3, 3, 'a3')
goselect t1.iEquimentId,t1.cMeascode,convert(varchar(13),t1.dtScadaTime,120)+':00:00' ScadaTime,t1.nAloge max_val,t1.dtScadaTime time_at_max,t2.nAloge min_val,t2.dtScadaTime time_at_min from
(
select m1.* from
(
select a.* from tb a where nAloge = (select max(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m1 where dtScadaTime =
(
select min(dtScadaTime) from
(
select a.* from tb a where nAloge = (select max(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m2
where m2.iEquimentId = m1.iEquimentId and m2.cMeascode = m1.cMeascode and convert(varchar(13),m2.dtScadaTime,120) = convert(varchar(13),m1.dtScadaTime,120)
)
) t1,
(
select m1.* from
(
select a.* from tb a where nAloge = (select min(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m1 where dtScadaTime =
(
select min(dtScadaTime) from
(
select a.* from tb a where nAloge = (select min(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m2
where m2.iEquimentId = m1.iEquimentId and m2.cMeascode = m1.cMeascode and convert(varchar(13),m2.dtScadaTime,120) = convert(varchar(13),m1.dtScadaTime,120)
)
) t2
where t1.iEquimentId = t2.iEquimentId and t1.cMeascode = t2.cMeascode and convert(varchar(13),t1.dtScadaTime,120) = convert(varchar(13),t2.dtScadaTime,120)
order by t1.iEquimentId , t1.cMeascode , convert(varchar(13),t1.dtScadaTime,120)drop table tb/*
iEquimentId cMeascode ScadaTime max_val time_at_max min_val time_at_min
----------- --------- ------------------- ------- ----------------------- ------- -----------------------
1 a1 2007-11-20 01:00:00 3.0 2007-11-20 01:01:03.000 1.0 2007-11-20 01:01:00.000
1 a1 2007-11-20 05:00:00 3.0 2007-11-20 05:01:00.000 1.0 2007-11-20 05:59:03.000
2 a2 2007-11-20 03:00:00 2.0 2007-11-20 03:21:03.000 1.0 2007-11-20 03:11:00.000
3 a3 2007-11-20 23:00:00 3.0 2007-11-20 23:01:00.000 3.0 2007-11-20 23:01:00.000(所影响的行数为 4 行)*/
insert into tb values('2007/11/20 01:01:00', 1, 1, 'a1')
insert into tb values('2007/11/20 01:01:03', 3, 1, 'a1')
insert into tb values('2007/11/20 01:59:03', 3, 1, 'a1')
insert into tb values('2007/11/20 05:01:00', 3, 1, 'a1')
insert into tb values('2007/11/20 05:01:03', 2, 1, 'a1')
insert into tb values('2007/11/20 05:59:03', 1, 1, 'a1')
insert into tb values('2007/11/20 03:11:00', 1, 2, 'a2')
insert into tb values('2007/11/20 03:21:03', 2, 2, 'a2')
insert into tb values('2007/11/20 03:59:03', 1, 2, 'a2')
insert into tb values('2007/11/20 23:01:00', 3, 3, 'a3')
insert into tb values('2007/11/20 23:01:03', 3, 3, 'a3')
insert into tb values('2007/11/20 23:59:03', 3, 3, 'a3')
goselect t1.cMeascode,t1.iEquimentId,convert(varchar(13),t1.dtScadaTime,120)+':00:00' ScadaTime,t1.nAloge max_val,t1.dtScadaTime time_at_max,t2.nAloge min_val,t2.dtScadaTime time_at_min from
(
select m1.* from
(
select a.* from tb a where nAloge = (select max(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m1 where dtScadaTime =
(
select min(dtScadaTime) from
(
select a.* from tb a where nAloge = (select max(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m2
where m2.iEquimentId = m1.iEquimentId and m2.cMeascode = m1.cMeascode and convert(varchar(13),m2.dtScadaTime,120) = convert(varchar(13),m1.dtScadaTime,120)
)
) t1,
(
select m1.* from
(
select a.* from tb a where nAloge = (select min(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m1 where dtScadaTime =
(
select min(dtScadaTime) from
(
select a.* from tb a where nAloge = (select min(nAloge) from tb where iEquimentId = a.iEquimentId and cMeascode = a.cMeascode and convert(varchar(13),dtScadaTime,120) = convert(varchar(13),a.dtScadaTime,120))
) m2
where m2.iEquimentId = m1.iEquimentId and m2.cMeascode = m1.cMeascode and convert(varchar(13),m2.dtScadaTime,120) = convert(varchar(13),m1.dtScadaTime,120)
)
) t2
where t1.iEquimentId = t2.iEquimentId and t1.cMeascode = t2.cMeascode and convert(varchar(13),t1.dtScadaTime,120) = convert(varchar(13),t2.dtScadaTime,120)
order by t1.iEquimentId , t1.cMeascode , convert(varchar(13),t1.dtScadaTime,120)drop table tb/*
cMeascode iEquimentId ScadaTime max_val time_at_max min_val time_at_min
--------- ----------- ------------------- ------- ----------------------- ------- -----------------------
a1 1 2007-11-20 01:00:00 3.0 2007-11-20 01:01:03.000 1.0 2007-11-20 01:01:00.000
a1 1 2007-11-20 05:00:00 3.0 2007-11-20 05:01:00.000 1.0 2007-11-20 05:59:03.000
a2 2 2007-11-20 03:00:00 2.0 2007-11-20 03:21:03.000 1.0 2007-11-20 03:11:00.000
a3 3 2007-11-20 23:00:00 3.0 2007-11-20 23:01:00.000 3.0 2007-11-20 23:01:00.000(所影响的行数为 4 行)
*/
insert into @t_scadaTMeas values('2007/11/20 01:01:00', 1, 1, 'a1')
insert into @t_scadaTMeas values('2007/11/20 01:01:03', 3, 1, 'a1')
insert into @t_scadaTMeas values('2007/11/20 01:59:03', 3, 1, 'a1')insert into @t_scadaTMeas values('2007/11/20 05:01:00', 3, 1, 'a1')
insert into @t_scadaTMeas values('2007/11/20 05:01:03', 2, 1, 'a1')
insert into @t_scadaTMeas values('2007/11/20 05:59:03', 1, 1, 'a1')insert into @t_scadaTMeas values('2007/11/20 03:11:00', 1, 2, 'a2')
insert into @t_scadaTMeas values('2007/11/20 03:21:03', 2, 2, 'a2')
insert into @t_scadaTMeas values('2007/11/20 03:59:03', 1, 2, 'a2')insert into @t_scadaTMeas values('2007/11/20 23:01:00', 3, 3, 'a3')
insert into @t_scadaTMeas values('2007/11/20 23:01:03', 3, 3, 'a3')
insert into @t_scadaTMeas values('2007/11/20 23:59:03', 3, 3, 'a3')select
a.cMeascode,
a.iEquimentId,
ScadaTime=convert(varchar(14),a.dtScadaTime,120)+'00:00',
max_val=max(a.nAloge),
time_at_max=min(a.dtScadaTime),
min_val=min(b.nAloge),
time_at_min=min(b.dtScadaTime)
from @t_scadaTMeas a join @t_scadaTMeas b on a.cMeascode=b.cMeascode and a.iEquimentId=b.iEquimentId and convert(varchar(14),a.dtScadaTime,120)=convert(varchar(14),b.dtScadaTime,120)
where a.nAloge=(select max(nAloge) from @t_scadaTMeas where cMeascode=a.cMeascode and iEquimentId=a.iEquimentId and convert(varchar(13),dtScadaTime,120)=convert(varchar(13),a.dtScadaTime,120))
and b.nAloge=(select min(nAloge) from @t_scadaTMeas where cMeascode=b.cMeascode and iEquimentId=b.iEquimentId and convert(varchar(13),dtScadaTime,120)=convert(varchar(13),b.dtScadaTime,120))
group by a.cMeascode,a.iEquimentId,convert(varchar(14),a.dtScadaTime,120)+'00:00'
/*
cMeascode iEquimentId ScadaTime max_val time_at_max min_val time_at_min
--------- ----------- ------------------- ------- ------------------------ ------- ------------------------
a1 1 2007-11-20 01:00:00 3.0 2007-11-20 01:01:03.000 1.0 2007-11-20 01:01:00.000
a1 1 2007-11-20 05:00:00 3.0 2007-11-20 05:01:00.000 1.0 2007-11-20 05:59:03.000
a2 2 2007-11-20 03:00:00 2.0 2007-11-20 03:21:03.000 1.0 2007-11-20 03:11:00.000
a3 3 2007-11-20 23:00:00 3.0 2007-11-20 23:01:00.000 3.0 2007-11-20 23:01:00.000
*/