sql server - How to get the data from same table by different conditions in SQL without UNION? -


i have below data in colorassign table;

| pkid | name    | type | hasassigned | | 11   | red     | 1    | true        | | 12   | blue    | 2    | false       | | 13   | yellow  | 3    | false       | | 14   | red1    | 1    | false       | | 15   | yellow1 | 3    | true        | | 16   | blue1   | 2    | false       | 

note in above table have color types in type field , assignstatus in hasassigned field different primary keys different names(can same name - type identifier find actual color).

my goal data condition:- find records color type hasassigned = true or if no records type has true. type hasassigned = false of color type. resultset be;

    | 11   | red     | 1    | true        |     | 12   | blue    | 2    | false       |     | 15   | yellow1 | 3    | true        |     | 16  | blue1   | 2    | false       | 

this possible union writing 2 queries, can 1 query. looking other optimized way?

in sql-server can use following query:

select pkid, name, type, hasassigned (    select pkid, name, type, hasassigned,           rank() on (partition type                         order case when hasassigned='true' 1 else 2 end) rnk    colorassign) t t.rnk = 1    

demo here


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -