需求如下:
log表
id    host           name         content        status    date
1    web主机       cpu使用率    cpu使用率5%       正常    2009-8-6
2    web主机       内存使用率   内存使用率20%     正常    2009-8-6
3    web主机       空间使用率   空间使用率39%     正常    2009-8-6
4    业务主机       cpu使用率    cpu使用率5%      正常    2009-8-6
5    业务主机       内存使用率   内存使用率20%    正常    2009-8-6
6    业务主机       空间使用率   空间使用率39%    正常    2009-8-6
7    接口主机       cpu使用率    cpu使用率5%      正常    2009-8-6
8    接口主机       内存使用率   内存使用率20%    正常    2009-8-6
9    接口主机       空间使用率   空间使用率39%    正常    2009-8-6
希望能统计处如下结果web主机状态  cup使用率   内存使用率   业务主机状态   cup使用率   内存使用率    接口主机状态   cup使用率   内存使用率
  正常           cpu使用率5%    内存使用率20% 正常           cpu使用率5%    内存使用率20%  正常           cpu使用率5%    内存使用率20%各位大侠,如何实现

解决方案 »

  1.   

    select decode(max(case when host='web主机' and status<>'正常' then status end),null,'正常','不正常')  web主机状态,
      max(case when host='web主机' and rn=1 then content end ) cpu使用率,
      max(case when host='web主机' and rn=2 then content end ) 内存使用率,
      decode(max(case when host='业务主机' and status<>'正常' then status end),null,'正常','不正常')  业务主机状态,
      max(case when host='业务主机' and rn=1 then content end)  cpu使用率,
      max(case when host='业务主机' and rn=2 then content end) 内存使用率,
      decode(max(case when host='接口主机' and status<>'正常' then status end),null,'正常','不正常')  接口主机状态,
      max(case when host='接口主机' and rn=1 then content end)  cpu使用率,
      max(case when host='接口主机' and rn=2 then content end)  内存使用率 from(
       select row_number()over(partition by host order by id) rn,
         host,name,content,status from test_h)--测试通过
    ID HOST NAME CONTENT STATUS
    1 web主机 cpu使用率 cpu使用率5% 正常
    2 web主机 内存使用率20% 正常
    3 web主机 空间使用率39% 正常
    4 业务主机 cpu使用率5% 正常
    5 业务主机 内存30% 正常
    6 业务主机 空间3% 正常
    7 接口主机 cpu 2% 正常
    8 接口主机 内存4% 不正常WEB主机状态 CPU使用率 内存使用率 业务主机状态 CPU使用率 内存使用率 接口主机状态 CPU使用率 内存使用率
    正常 cpu使用率5% 内存使用率20% 正常 cpu使用率5% 内存30% 不正常 cpu 2% 内存4%
      

  2.   

    如果用一条sql语句实现1楼的可行。
    当然也可以用存储过程实现。
      

  3.   


    那是因为你要的结果字段很多。不过都大同小异,那几个case其实也就换个参数而已
      

  4.   

    select 
    a.date "日期", 
    case when ws=3 then '正常' else '不正常' end "web主机状态",
    ws1 "cpu使用率",
    ws2 "内存使用率",
    ws3 "空间使用率",
    case when bs=3 then '正常' else '不正常' end "业务主机状态",
    ws1 "cpu使用率",
    ws2 "内存使用率",
    ws3 "空间使用率",
    case when is_=3 then '正常' else '不正常' end "接口主机状态",
    ws1 "cpu使用率",
    ws2 "内存使用率",
    ws3 "空间使用率"
    from (
    select date, 
    sum(case when host='web主机' and status='正常' then 1 else 0 end) ws, 
    max(case when host='web主机' and name='cpu使用率' then  content else '' end) ws1, 
    max(case when host='web主机' and name='内存使用率' then  content else '' end) ws2, 
    max(case when host='web主机' and name='空间使用率' then  content else '' end) ws3,sum(case when host='业务主机' and status='正常' then 1 else 0 end) bs, 
    max(case when host='业务主机' and name='cpu使用率' then  content else '' end) bs1, 
    max(case when host='业务主机' and name='内存使用率' then  content else '' end) bs2, 
    max(case when host='业务主机' and name='空间使用率' then  content else '' end) bs3, sum(case when host='接口主机' and status='正常' then 1 else 0 end) is_, 
    max(case when host='接口主机' and name='cpu使用率' then  content else '' end) is1, 
    max(case when host='接口主机' and name='内存使用率' then  content else '' end) is2, 
    max(case when host='接口主机' and name='空间使用率' then  content else '' end) is3from machine_status group by date) a