sql - conditional where clause -


i need filter result of join query between 2 tables don't have condition "where clause".

what need filter based on id_project this:

  1. if id_project equal 24 (24 default project) should return rows id_project =24. here rows 1,3...10 selected

  2. if id_project equal 25, need rows have id_project=25 plus rows has " id_project=24 , not id_project 25, so rows number 2 11 selected

with query :

select tp.id_tag, tp.id_project, tp.njtagname, tp.node_level , tl.id_level     instrumentation.dbo.tag_project tp     inner join instrumentation.dbo.tag_level tl     on tl.id_tag=tp.id_tag //  tl.id_level=69  , tp.node_level=1  

i result :

enter image description here

how can change query this?

thinking little more request, burns down to: every id_tag give me id requested, or 24 if not available. can done in 1 query, use ranking row_number, in prefer requested id on 24.

select * (   select     tp.*,     row_number() over(partition id_tag                        order case when id_project = 24 2 else 1 end) rn   tag_project tp   id_project in (24, @requested_id) ) ranked rn = 1; 

here original query changed accordingly:

select id_tag, id_project, njtagname, node_level, id_level (   select tp.id_tag, tp.id_project, tp.njtagname, tp.node_level , tl.id_level        , row_number() on (partition tp.id_tag order case when tp.id_project = 24 2 else 1 end) rn        instrumentation.dbo.tag_project tp       inner join instrumentation.dbo.tag_level tl on tl.id_tag=tp.id_tag   tl.id_level=69 , tp.node_level=1    , tp.id_project in (24, @requested_id) ) ranked rn = 1; 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -