繁体   English   中英

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

[英]Using case in where clause with conditions in SQL

我写了下面的查询以根据条件花费时间:

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;

因此,目标是:
如果该值> = 8mins(0.133 hrs),则需要上几节课,而对于不属于该组的其他班级,我的小时数必须> = 5min(0.0833 hrs)。 我已经使用CASE尝试了上述方法,但不确定。 这样没有错误,但是如何确保获得正确的值。 有人可以帮我吗?

你为什么这么复杂? 如果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

那不是吗

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