sql server - distinct merging select results in mssql keeping order of each results -
my website sort of dictionary. when keyword searched want in both 'word' , 'meaning' column , display matches either word or meaning.
select count(id) words word @keyword or meaning @keyword select * words word @keyword or meaning @keyword order word
but want first show matching words , matching meanings. order not correct
when separate them:
select count(id) words word @keyword select * words word @keyword order word select count(id) words meaning @keyword select * words meaning @keyword order word
this way there duplicates (when keyword matches both word , meaning)
and when union them again order won't correct
how can done? need count of distinct matching results + distinct matching results first showing matching words , matching meanings.
use case statement in order clause distinguish when it's match on word:
select * words word @keyword or meaning @keyword order case when word @keyword 0 else 1 end , word