sql server - condition based select SQL query -


for example have table "info" below.

+--------+------------+------+------------+ | entity | department | code | code_count | +--------+------------+------+------------+ | e1     | d1         |  123 |          5 | | e1     | d1         |  234 |         10 | | e1     | d1         |  345 |         20 | | e1     | d2         |  456 |          2 | | e1     | d2         |  567 |          5 | | e1     | d2         |  678 |         10 | +--------+------------+------+------------+  

my query should function this: each entity , department pair, select code has maximum code count.

any appreciated. thanks.

select t1.* your_table t1 join (   select entity, department, max(code_count) code_count    your_table   group entity, department ) t2 on t1.entity = t2.entity      , t1.department = t2.department      , t1.code_count = t2.code_count  

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -