order - How to make a SQL request ordered by amount of true conditions -


imagine have 10 conditions in 'where' clausule of 'select' request.

i need show results satisfied @ least 5 of conditions, , ordered amount of satisfied conditions: 10,9,8...

how can posible?

thanks lot :)

this may vary based on dbms you're using, ms-sql, clause segmented separate case statements summed , sorted:

select     *,     case when (condition 1) 1 else 0 end     + case when (condition 2) 1 else 0 end     (...)     + case when (condition 10) 1 else 0 end matchcount mytable order matchcount desc 

you can wrap sub-query results matching @ least 5:

select * (     select         *,         case when (condition 1) 1 else 0 end         + case when (condition 2) 1 else 0 end         (...)         + case when (condition 10) 1 else 0 end matchcount     mytable) t t.matchcount >= 5 order matchcount desc 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -