22,298
社区成员
发帖
与我相关
我的任务
分享
SELECT A.STCD,CONVERT(VARCHAR(10),A.TM,120)AS DT,
AVG(C.VVSWC)AS DASMC,
B.VVSWC AS DMXSMC, B.TM AS DMXSMCOT,
A.VVSWC AS DMNSMC, A.TM AS DMNSMCOT
FROM
(SELECT *,RN=(SELECT COUNT(1)+1 FROM #SM_SLMC_R_D WHERE VVSWC<a.VVSWC and STCD=a.STCD)FROM #SM_SLMC_R_D a) A
JOIN
(SELECT *,RN=(SELECT COUNT(1)+1 FROM #SM_SLMC_R_D WHERE VVSWC>a.VVSWC and STCD=a.STCD)FROM #SM_SLMC_R_D a) B
ON A.STCD=B.STCD,
#SM_SLMC_R_D C
WHERE A.RN=1 AND B.RN=1
GROUP BY A.STCD,B.STCD,A.TM,B.TM,A.VVSWC,B.VVSWC
AVG(C.VVSWC)AS DASMC, 时,算出的总是所有值的平均值,而不是单一STCD的 #SM_SLMC_R_D C 该如何约束create table tb(STCD varchar(10),TM datetime, VVSWC decimal(18,1))
insert into tb values('60436100' , '2009-09-16 01:00:00' , 1.3)
insert into tb values('60436100' , '2009-09-16 02:00:00' , 1.2)
insert into tb values('60436100' , '2009-09-16 03:00:00' , 1.1)
insert into tb values('60436100' , '2009-09-16 04:00:00' , 1.3)
insert into tb values('60436100' , '2009-09-16 05:00:00' , 1.1)
insert into tb values('60436100' , '2009-09-16 01:00:00' , 1.4)
insert into tb values('60436200' , '2009-09-16 02:00:00' , 1.5)
insert into tb values('60436200' , '2009-09-16 03:00:00' , 1.3)
go
--如果最大值或最小值出现相同时间时,取最小的时间
select STCD ,
convert(varchar(10),tm,120) dt ,
cast(avg(VVSWC) as decimal(18,1)) DASMC ,
max(VVSWC) DMXSMC ,
(select top 1 TM from tb where STCD = t.STCD and convert(varchar(10),tm,120) = convert(varchar(10),t.tm,120) and VVSWC = (select max(VVSWC) from tb where STCD = t.STCD and convert(varchar(10),tm,120) = convert(varchar(10),t.tm,120)) order by TM) DMXSMCOT,
min(VVSWC) DMNSMC ,
(select top 1 TM from tb where STCD = t.STCD and convert(varchar(10),tm,120) = convert(varchar(10),t.tm,120) and VVSWC = (select min(VVSWC) from tb where STCD = t.STCD and convert(varchar(10),tm,120) = convert(varchar(10),t.tm,120)) order by TM) DMNSMCOT
from tb t
group by STCD ,convert(varchar(10),tm,120)
/*
STCD dt DASMC DMXSMC DMXSMCOT DMNSMC DMNSMCOT
---------- ---------- -------------------- -------------------- ------------------------------------------------------ -------------------- ------------------------------------------------------
60436100 2009-09-16 1.2 1.4 2009-09-16 01:00:00.000 1.1 2009-09-16 03:00:00.000
60436200 2009-09-16 1.4 1.5 2009-09-16 02:00:00.000 1.3 2009-09-16 03:00:00.000
(所影响的行数为 2 行)
*/
--如果最大值或最小值出现相同时间时,取最大的时间
select STCD ,
convert(varchar(10),tm,120) dt ,
cast(avg(VVSWC) as decimal(18,1)) DASMC ,
max(VVSWC) DMXSMC ,
(select top 1 TM from tb where STCD = t.STCD and convert(varchar(10),tm,120) = convert(varchar(10),t.tm,120) and VVSWC = (select max(VVSWC) from tb where STCD = t.STCD and convert(varchar(10),tm,120) = convert(varchar(10),t.tm,120)) order by TM desc) DMXSMCOT,
min(VVSWC) DMNSMC ,
(select top 1 TM from tb where STCD = t.STCD and convert(varchar(10),tm,120) = convert(varchar(10),t.tm,120) and VVSWC = (select min(VVSWC) from tb where STCD = t.STCD and convert(varchar(10),tm,120) = convert(varchar(10),t.tm,120)) order by TM desc) DMNSMCOT
from tb t
group by STCD ,convert(varchar(10),tm,120)
/*
STCD dt DASMC DMXSMC DMXSMCOT DMNSMC DMNSMCOT
---------- ---------- -------------------- -------------------- ------------------------------------------------------ -------------------- ------------------------------------------------------
60436100 2009-09-16 1.2 1.4 2009-09-16 01:00:00.000 1.1 2009-09-16 05:00:00.000
60436200 2009-09-16 1.4 1.5 2009-09-16 02:00:00.000 1.3 2009-09-16 03:00:00.000
(所影响的行数为 2 行)
*/
drop table tb
[Quote=引用 2 楼 feixianxxx 的回复:]