tsql - Find matching records in same table different columns -


i have query:

select     tlocmtr.locatn_k, tlocmtr.mtr_srp_n, tlocmtr.mtr_inst_d,      tlocmtr.mtr_xchg_out_d, rate.rate, tmtr.mtr_mfr_mod_c,      tmtr.mtr_mfr_nm_cd     tmtr  right outer join     tlocmtr on tmtr.mtr_srp_n = tlocmtr.mtr_srp_n  left outer join     rate on tlocmtr.locatn_k = rate.locatn_k     ((rate.rate >= 20) , (rate.rate <= 29))      , tlocmtr.mtr_inst_d >= '2014-01-01'  order     locatn_k, mtr_inst_d 

the code results in:

enter image description here

what need count number of times mtr_mfr_nm_cd changes amp <>amp per locatn_k mtr_inst_d = mtr_xchg_out_d. example locatn_k = 85420005 went amp eem , mtr_xchg_d = mtr_inst_d 1 time (date '2014-08-27'. locatn_k 85430001 went amp eem 2 times on dates '2015-01-05' , '2016-01-12.

please me t-sql answer this!

this essentials

select *    ( select locatn_k, mtr_inst_d, mtr_mfr_nm_cd                , lead(mtr_mfr_nm_cd, 1,0) on (partittion locatn_k order mtr_inst_d) mtr_mfr_nm_cd_next                        ...         ) tt  mtr_mfr_nm_cd      =  'amp'    , mtr_mfr_nm_cd_next <> 'amp' 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -