34,838
社区成员




Select
STCD,
TM
from
ST_RIVER_R a
where
not exists(select 1 from ST_RIVER_R where stcd = a.stcd and TM > a.TM)
and STCD in ('40609500','40609502','40609504','40609506','40609508','40609510','40609512','40609514','40610101','40610102','40610104','40610106','40610108','40610110','40610112','40610400','40610402','40610404','40610406','40610408','40610410','40610412','40610414','40610450','40610452','40610454','40610456','40610458','40610460','40610462','40610464','40610466','40610468','40610470','40610472','40610500','40610502','40610504','40610506','40804000','40804002','40804004','40804006','40804008','40804010','40804100','40804102','40804104','40804106','40804108','40804110','40804112','40804200','40804202','40804204','40804206','40804208','40804210','40804212','40804214','40804216','40902840','40902842','41110700','41110702','41110704','41110706','41110750','41110752','41110754','41111050','41111075','41111077','41111079','41111081','41111083','41111085','41111087','41111089','41111091','41111150','41111152','41111154','41111156','41111158','41111160','41111162','41111164','41111166','41111168','41111170','41111172','41111250','41111252','41111254','41111256','41111351','41111352','41111354','41111356','41111358','41111360','41111362','41111364','41111380','41111382','41111384','41111386','41111520','41111522','41111524','41111526','41111528','41111530','41111532','41111534','41111536','41111538','41111540','41111542','41111550','41111552','41111554','41111556','41209100','41209102','41209104','41209106','41209108','41209110','41209112','41209114','41209116','41209118','41209120','41209122','41209124','41209126','41209300','41209350','41209352','41209450','41312000','41312002','41312004','41312006','41312008','41312010','41312012','41312014','41313500','41313502','41313504','41313506','41313508','41313510','41313512','41610000','41610002','41610004','60719100','60719102','60719104','60719106','60719108','60719110','60719112','60719114','60719116','60719118','60719120','60719122','60719131','60719132','60719134','61814200','61814202','61814204','61814206','61814208','61815800','61815802','61815804','61815806','61815808','61815810','61815850','61815852','61815854','61815856','61815858','61815860','61815862','61815864','61816000','61816002','61816004','61816006','61816008','61816010','61816012','61817000','61817002','61817004','61817006','61817008','61817010','61817012','61817014','61817020','61817022','61919801','61919802','61919804','61919806','61919808') and TM between '2013-03-09 08:00:00' and '2013-03-25 08:00:00'
ORDER BY STCD,TM
select stcd,max(TM) TM
into #temp
from ST_RIVER_R
group by stcd
Select
STCD,
TM
from
ST_RIVER_R a
join #temp b
on a.stcd = b.stcd and a.TM=b.TM
and TM between '2013-03-09 08:00:00' and '2013-03-25 08:00:00'
and STCD in ('40609500','40609502','40609504','40609506','40609508','40609510','40609512','40609514','40610101','40610102','40610104','40610106','40610108','40610110','40610112','40610400','40610402','40610404','40610406','40610408','40610410','40610412','40610414','40610450','40610452','40610454','40610456','40610458','40610460','40610462','40610464','40610466','40610468','40610470','40610472','40610500','40610502','40610504','40610506','40804000','40804002','40804004','40804006','40804008','40804010','40804100','40804102','40804104','40804106','40804108','40804110','40804112','40804200','40804202','40804204','40804206','40804208','40804210','40804212','40804214','40804216','40902840','40902842','41110700','41110702','41110704','41110706','41110750','41110752','41110754','41111050','41111075','41111077','41111079','41111081','41111083','41111085','41111087','41111089','41111091','41111150','41111152','41111154','41111156','41111158','41111160','41111162','41111164','41111166','41111168','41111170','41111172','41111250','41111252','41111254','41111256','41111351','41111352','41111354','41111356','41111358','41111360','41111362','41111364','41111380','41111382','41111384','41111386','41111520','41111522','41111524','41111526','41111528','41111530','41111532','41111534','41111536','41111538','41111540','41111542','41111550','41111552','41111554','41111556','41209100','41209102','41209104','41209106','41209108','41209110','41209112','41209114','41209116','41209118','41209120','41209122','41209124','41209126','41209300','41209350','41209352','41209450','41312000','41312002','41312004','41312006','41312008','41312010','41312012','41312014','41313500','41313502','41313504','41313506','41313508','41313510','41313512','41610000','41610002','41610004','60719100','60719102','60719104','60719106','60719108','60719110','60719112','60719114','60719116','60719118','60719120','60719122','60719131','60719132','60719134','61814200','61814202','61814204','61814206','61814208','61815800','61815802','61815804','61815806','61815808','61815810','61815850','61815852','61815854','61815856','61815858','61815860','61815862','61815864','61816000','61816002','61816004','61816006','61816008','61816010','61816012','61817000','61817002','61817004','61817006','61817008','61817010','61817012','61817014','61817020','61817022','61919801','61919802','61919804','61919806','61919808')
ORDER BY STCD,TM
试试这样
with t
as
(
Select STCD,TM,ROW_NUMBER() OVER(PARTITION BY stcd ORDER BY TM DESC) as num from ST_RIVER_R
where STCD in ('40609500','40609502','40609504','40609506','40609508','40609510','40609512','40609514','40610101','40610102','40610104','40610106','40610108','40610110','40610112','40610400','40610402','40610404','40610406','40610408','40610410','40610412','40610414','40610450','40610452','40610454','40610456','40610458','40610460','40610462','40610464','40610466','40610468','40610470','40610472','40610500','40610502','40610504','40610506','40804000','40804002','40804004','40804006','40804008','40804010','40804100','40804102','40804104','40804106','40804108','40804110','40804112','40804200','40804202','40804204','40804206','40804208','40804210','40804212','40804214','40804216','40902840','40902842','41110700','41110702','41110704','41110706','41110750','41110752','41110754','41111050','41111075','41111077','41111079','41111081','41111083','41111085','41111087','41111089','41111091','41111150','41111152','41111154','41111156','41111158','41111160','41111162','41111164','41111166','41111168','41111170','41111172','41111250','41111252','41111254','41111256','41111351','41111352','41111354','41111356','41111358','41111360','41111362','41111364','41111380','41111382','41111384','41111386','41111520','41111522','41111524','41111526','41111528','41111530','41111532','41111534','41111536','41111538','41111540','41111542','41111550','41111552','41111554','41111556','41209100','41209102','41209104','41209106','41209108','41209110','41209112','41209114','41209116','41209118','41209120','41209122','41209124','41209126','41209300','41209350','41209352','41209450','41312000','41312002','41312004','41312006','41312008','41312010','41312012','41312014','41313500','41313502','41313504','41313506','41313508','41313510','41313512','41610000','41610002','41610004','60719100','60719102','60719104','60719106','60719108','60719110','60719112','60719114','60719116','60719118','60719120','60719122','60719131','60719132','60719134','61814200','61814202','61814204','61814206','61814208','61815800','61815802','61815804','61815806','61815808','61815810','61815850','61815852','61815854','61815856','61815858','61815860','61815862','61815864','61816000','61816002','61816004','61816006','61816008','61816010','61816012','61817000','61817002','61817004','61817006','61817008','61817010','61817012','61817014','61817020','61817022','61919801','61919802','61919804','61919806','61919808') and TM between '2013-03-09 08:00:00' and '2013-03-25 08:00:00'
)
select * from t where num = 1