注册 登录
编程论坛 SQL Server论坛

达人们救命啊

qisong123456 发布于 2008-07-25 10:44, 1581 次点击
小弟执行下面这段程序,总是报错,请求指点,不甚感激
select alarmGrade, stattime,  cfg.ipaddr, cfg.sevice,  
         case alarmItem when 1 then '非关键业务带宽占用率'+valveExp+valveValue+'%'
                        else '非关键业务带宽占用量'+valveExp+valveValue end as alarmCond,
         case alarmItem when 1 then 1-sum(sendbytes+recvbytes)/200000 else sum(sendbytes+recvbytes) end as alarmValue
from alarm_cfg as cfg, user_traffic as uData
where cfg.sevice=uData.sevice and case  when cfg.ipaddr=-1 then 1 else cfg.ipaddr=uData.ipaddr end and stattime>100
group by cfg.sevice, cfg.ipaddr, cfg.alarmGrade, cfg.alarmItem, cfg.valveExp, cfg.valveValue
having (case alarmItem when 1
    then case valveExp when '=' then 1-sum(sendbytes+recvbytes)/200000=valveValue
             when '<' then 1-sum(sendbytes+recvbytes)/200000<valveValue
            when '>' then 1-sum(sendbytes+recvbytes)/200000>valveValue
            else 1-sum(sendbytes+recvbytes)/200000<>valveValue
            end
    else case valveExp when '=' then sum(sendbytes+recvbytes)=valveValue
            when '<' then sum(sendbytes+recvbytes)<valveValue
            when '>' then sum(sendbytes+recvbytes)>valveValue
            else  sum(sendbytes+recvbytes)<>valveValue
        end
        end=1);
11 回复
#2
happynight2008-07-25 10:54
报什么错,
你查询中CASE怎么有两个WHEN 1
Having中的字段必须存在于SELECT中吧,你这样复杂的写法没用过,最好吧需求发上来 看有无必要写的这么复杂
#3
happynight2008-07-25 10:55
建议你去http://www.发帖 那里高手很多
#4
qisong1234562008-07-25 10:59
两个when 1是两个属性的判断,这里倒是没报错,后面的where cfg.sevice=uData.sevice and case  when cfg.ipaddr=-1 then 1 else cfg.ipaddr=uData.ipaddr end and stattime>100
有错误
#5
happynight2008-07-25 11:23
case  when cfg.ipaddr=-1 then 1 else cfg.ipaddr=uData.ipaddr end
改成
cfg.ipaddr= (case when -1 then 1 else udata.ipadrr end)
试下 你那样的Having写法还真是没见过
#6
qisong1234562008-07-25 11:24
呵呵,我试试,谢谢你啊
#7
happynight2008-07-25 11:27
呵呵 客气 有空多交流下
#8
qisong1234562008-07-25 11:31
还是不行啊,case  when cfg.ipaddr=-1 then 1 else cfg.ipaddr=uData.ipaddr end 这句我的本意是当cfg.ipaddr=-1时,这个条件就为真不判断了,否则就判断cfg.ipaddr=uData.ipaddr这个条件
#9
happynight2008-07-25 11:32
==调试下
#10
qisong1234562008-07-25 11:35
恩,麻烦你了
#11
happynight2008-07-25 11:41
哦 你改成
cfg.ipaddr= (case when -1 then cfg.ipaddr else udata.ipadrr end)
试下
#12
qisong1234562008-07-25 11:45
好了,好了,真是太感谢你了
1