简体   繁体   中英

Using case in where clause with conditions in SQL

I have written the below query to take the hours based on conditions:

select  h.device_id,
        h.month_id,
        h.group,
        h.hours,
        t.class
from    dbo.network_hours h,
        dbo.monthly_class t
where   h.device_id = t.device_id
        and h.month_id = t.month_id
        and
        case when h.group IN ('Adult','Zone','Family','Latino','Comedy','West')
             then h.hours >= 0.13333                                
        end h.hours >= 0.08333;

So the objective is :
There are a few classes whose hours I need to take if that value is >= 8mins(0.133 hrs) and for other classes which do not fall in the group the hours have to be >= 5min(0.0833 hrs). I have tried the above using CASE but not sure. There is no error as such but how do I make sure I am getting the correct values. Can someone please help me with this?

Why you do it so complex? Try replace case with h.hours >= 0.08333 or (h.group in ('Adult'....) and h.hours>=0.13333)) or if you preferred to use case h.hours >= case when h.group in ('Adult'....) then 0.13333 else 0.08333 end

Wouldn't that be

where h.hours >= 
  case when h.group in ('Adult','Zone','Family','Latino','Comedy','West') then 0.13333
       else 0.08333
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM