switch单独一个表,有switch_code
switch_port单独一个表,有switch_code,switch_port_code,status.(status有:U(in use),R,D,A四种情况)
如何求出每个switch中port口占用率?

解决方案 »

  1.   

    WITH t AS 
    (
    SELECT 'A' switch_code, 'u' status FROM dual
    UNION ALL
    SELECT 'A' , 'r' status FROM dual
    UNION ALL
    SELECT 'A' , 'd' status FROM dual
    UNION ALL
    SELECT 'A' , 'a' status FROM dual
    UNION ALL
    SELECT 'A' , 'd' status FROM dual
    UNION ALL
    SELECT 'A' , 'u' status FROM dual
    UNION ALL
    SELECT 'A' , 'u' status FROM dual
    UNION ALL
    SELECT 'A' , 'u' status FROM dual
    )SELECT switch_code,
           SUM(decode(status,'u',1,0))/COUNT(switch_code) u占有率,
           SUM(decode(status,'a',1,0))/COUNT(switch_code) a占有率,
           SUM(decode(status,'d',1,0))/COUNT(switch_code) d占有率,
           SUM(decode(status,'r',1,0))/COUNT(switch_code) r占有率
           FROM t
           GROUP BY switch_code--result:
    A 0.5 0.125 0.25 0.125
      

  2.   

    谢谢你的回复。
    whith as怎么用的啊?我想要的是:select
    (
    (select count(*) from switch_port a where a.switch_code=b.switch_code and a.status='U')/sum(c.switch_port_code)*100
    (
    from switch b,switch_port c 
    where b.switch_code=c.switch.code这样来求出每个switch中port的占用率。但是不行啊。
    ORA-00937: not a single-group group function
      

  3.   

    select sum(decode(status,'U',1,0))/count(1)*100
    from switch_port c 
    group by switch_code
      

  4.   


    Good job!多谢指教!用的妙!