sql server continuous rainfall days and start and end dates in a year

 select
     min(tm) 'startday',
     max(tm) 'endday',
     count(1) 'lxday',
     SUM(avgz) avgz
 from
 (


  select a.tm,a.avgz,
   datepart(Y,a.tm)-
     (
       select count(1) from
      (
     select * from (
select datepart(mm,ts.TM) as mm,  datepart(dd,ts.TM) as dd,avg(ts.DRP) as avgz,ts.tm from ST_PPTN_DAY_R ts left join ST_STBPRP_B f on ts.STCD = f.stcd where ts.TM>='2016-01-01 00:00:00' and ts.TM<'2016-12-31 23:59:59'  group by datepart(mm,ts.TM) ,datepart(dd,ts.TM),ts.tm
) ag  where avgz >0

      ) b
     where  b.tm<=a.tm
        
   ) 'rn'
    from
    (
     select * from (
select datepart(mm,ts.TM) as mm,  datepart(dd,ts.TM) as dd,avg(ts.DRP) as avgz,ts.tm from ST_PPTN_DAY_R ts left join ST_STBPRP_B f on ts.STCD = f.stcd where ts.TM>='2016-01-01 00:00:00' and ts.TM<'2016-12-31 23:59:59'  group by datepart(mm,ts.TM) ,datepart(dd,ts.TM),ts.tm
) ag  where avgz >0

    )a
   
 
 ) t
 
 group by rn;

 

Average rainfall at 8 rainfall stations

select datepart(mm,ts.TM) as mm,  datepart(dd,ts.TM) as dd,avg(ts.DRP) as avgz,ts.tm from ST_PPTN_DAY_R ts left join ST_STBPRP_B f on ts.STCD = f.stcd where ts.TM>='2016-01-01 00:00:00' and ts.TM<'2016-12-31 23:59:59' group by datepart(mm,ts.TM) ,datepart(dd,ts.TM),ts.tm

 


 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326818696&siteId=291194637