sql server - How do I retrieve a count of timestamps over the first hour of each shift? -


okay 1 might tricky explain..

what trying count number of loads within first hour of each shift. mean if loading starts @ 0630, want count number of times finished loading 0630 0700. if loading started @ 0705, count 0705 0800 etc.

here's have work far:

select est.shift_date     ,case est.shift_ident         when '1' 'd'         when '2'then 'n'         end shift_ident     ,min(est.start_timestamp) start equip_status_trans est join equip_status_code esc on esc.status_code est.status_code est.shift_date >= (getdate() - 180)     , esc.status_desc 'lu loading'     , est.equip_ident in (         's803'         ,'s804'         ) group est.shift_date     ,est.shift_ident     ,est.equip_ident 

ignoring filters, gets me date, shift identity, , start timestamp of loading each shift.

i have

select est.shift_date     ,case est.shift_ident         when '1' 'd'         when '2' 'n'         end shift_ident     ,count(est.end_timestamp) count     ,min(est.start_timestamp) start equip_status_trans est join equip_status_code esc on esc.status_code = est.status_code est.shift_date >= (getdate() - 180)     , esc.status_desc = 'lu loading'     , est.equip_ident in (         's803'         ,'s804'         ) group est.shift_date     ,est.shift_ident     ,est.equip_ident     ,datepart(hh, est.end_timestamp) 

which gives me date, shift identity, start timestamp of loading, , load count each hour. start timestamps minimum loading timestamps each hour instead of minimum timestamp each shift.

the problem i'm having every time try , join two, returns load count every hour during shift instead of first hour.

as new sql server, easiest way go this?

much appreciated


here's join statement looks like

 select est.shift_date,      case est.shift_ident          when '1' 'd'          when '2' 'n'          end shift_ident,      min(est.start_timestamp) start     , est.equip_ident, d1.count  equip_status_trans est  join equip_status_code esc on esc.status_code = est.status_code  left outer join     (select est2.shift_date,          case est2.shift_ident              when '1' 'd' when '2' 'n'              end shift_ident         , count(est2.end_timestamp) count         , min(est2.start_timestamp) start         , est2.equip_ident     equip_status_trans est2      join equip_status_code esc2 on esc2.status_code = est2.status_code     est2.shift_date >= (getdate() - 180)          , esc2.status_desc = 'lu loading'          , est2.equip_ident in('s803', 's804')     group est2.shift_date         , est2.shift_ident         , est2.equip_ident         , datepart(hh, est2.end_timestamp)     )d1 on est.start_timestamp = d1.start est.shift_date >= (getdate() - 180)  , esc.status_desc = 'lu loading'  , est.equip_ident in('s803', 's804') group est.shift_date, est.shift_ident, est.equip_ident, d1.count 

this more of pseudo-sql abstracts-away of select data not pertinent join , filters, believe you'll want this:

with start_time_per_shift (/*select statement gives minimum start per shift */)  select est.shift_date,        count(est.end_timestamp) count,        st_time.start start   equip_status_trans est   join start_time_per_shift st_time     on est.shift_date = st_time.shift_date    , datepart(hh, est.end_timestamp) =        datepart(hh, est.shift_date) --get next hour shift start; join records within first hour  group est.shift_date st_time.start 

perhaps this...

with start_time_per_shift  (select est.shift_date,          case est.shift_ident            when '1'             'd'            when '2'             'n'          end shift_ident,          /*est.equip_ident, (did need grouping well?)*/          min(est.start_timestamp) start     equip_status_trans est     join equip_status_code esc       on esc.status_code est.status_code    est.shift_date >= (getdate() - 180)      , esc.status_desc 'lu loading'      , est.equip_ident in ('s803', 's804')    group est.shift_date, est.shift_ident   /*,est.equip_ident*/   )  select est.shift_date,        case est.shift_ident          when '1'           'd'          when '2'           'n'        end shift_ident,        /*est.equip_ident, (did need grouping well?) */        count(est.end_timestamp) count,        st_time.start start   equip_status_trans est   join start_time_per_shift st_time     on est.shift_date = st_time.shift_date    , case est.shift_ident when '1' 'd' when '2' 'n' end = st_time.shift_ident  /*and est.equip_ident = st_time.equip_ident*/   join equip_status_code esc     on esc.status_code = est.status_code  est.shift_date >= (getdate() - 180)    , datepart(hh, est.shift_date) = datepart(hh, est.end_timestamp)    , esc.status_desc = 'lu loading'    , est.equip_ident in ('s803', 's804')  group est.shift_date, est.shift_ident, /*est.equip_ident,*/ st_time.start 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -