0

Can Somebody help me with following sql query.

When you want to filter by Phase I, It should return results with Phase I or Phase I/II but not Phase II or Phase III.

Similarly for Phase II, it should return results with Phase I/II, Phase II or Phase II/III

select Phase from Phases where Phase in (case 

when @phase='Phase I' then '''Phase I'',''Phase I/II'''
when @phase='Phase II' then '''Phase I/II'',''Phase II'',''Phase II/III'''
end)

I do not want to use dynamic query to solve the issue.

4

1 回答 1

3

布尔逻辑可以解决这个问题:

select Phase 
  from Phases 
 where (@phase = 'Phase I'  and Phase in ('Phase I', 'Phase I/II'))
    or (@phase = 'Phase II' and Phase in ('Phase I/II', 'Phase II', 'Phase II/III'))
于 2013-10-16T13:17:39.327 回答