how to select which id comes second the most often in sql server -


i have table so:

create table t1 ( id_a int id_b int dt datetime ) 

sample data might be:

id_a  id_b  dt  39838 6     2014-01-21 11:20:29.537 39838 546   2014-01-21 11:20:29.790 39839 4088  2014-01-21 11:20:31.543 39795 6     2014-01-21 11:20:33.117 39795 546   2014-01-21 11:20:34.100 39795 3189  2014-01-21 11:20:35.520 39841 6     2014-01-21 11:20:36.957 39841 7588  2014-01-21 11:20:38.030 

i want sql tell me id_b follows id_b of 6 (by follows mean dt) id_a

for sample data above, id_b 546 follow 6 twice same id_a , 7588 follows 6 once same id_a, output looking in case 546

i hope i've made clear, can me how i'd write sql that?

something effect:

select most_common(id_b) t1 previous_entry(id_b) = 6   , previous_entry(id_a) = this_entry(id_a) order id_a, dt 

you can accomplish using parition clause:

select  ib_b, count(id_a) no_of_times (select *, row_number() on (partition id_a order dt) row_no t1)temp row_no = (select top 1 row_no+1 (select *, row_number() on (partition id_a order dt) row_no t1)temp ib_b =6 ) group ib_b order count(id_a) desc 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -