mysql - SQL statement to count corresponding entries while having a strict sequence (logical) -


i want aggregate number of (identical) parts in production plan while preserving sequence.

my table looks this:

------------------------------------------------- |part number | production sequence number | .... | ------------------------------------------------- |           1|                           1| .... | -------------------------------------------------- |           1|                           2| .... | -------------------------------------------------- |           2|                           3| .... | -------------------------------------------------- |           2|                           4| .... | -------------------------------------------------- |           1|                           5| .... | -------------------------------------------------- 

and need count amount of same parts in row:

expected result:

------------------------------------------------- |part number | nr of pieces in row      | .... | ------------------------------------------------- |           1|                           2| .... | ------------------------------------------------- |           2|                           2| .... | ------------------------------------------------- |           1|                           1| .... | ------------------------------------------------- 

can done unsing sql (mysql)?

you may use variable emulate numbers , perform group by

select id, count(*) (     select          @row_number:=case             when @seq_num = id @row_number             else @row_number + 1         end num,         @seq_num:=id id,         seq_num     tab, (select @row_number:=0, @seq_num := 1) t     order seq_num ) t group num, id 

demo


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -