简体   繁体   English

在SQL中带条件的where子句中使用案例

[英]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). 如果该值> = 8mins(0.133 hrs),则需要上几节课,而对于不属于该组的其他班级,我的小时数必须> = 5min(0.0833 hrs)。 I have tried the above using CASE but not sure. 我已经使用CASE尝试了上述方法,但不确定。 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 如果h.hours >= 0.08333 or (h.group in ('Adult'....) and h.hours>=0.13333))请尝试替换case,或者如果您更喜欢h.hours >= case when h.group in ('Adult'....) then 0.13333 else 0.08333 end使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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