日期:2014-05-16  浏览次数:20397 次

oracle常用方法之行转列
select MAX(LTRIM(SYS_CONNECT_BY_PATH(rs.ip, ','), ',')) IP_ADDRESS
  from (select t.ip,rownum RN
          from t_ip_list t
          ) rs
START WITH rs.RN = 1
CONNECT BY PRIOR rs.RN + 1 = rs.RN
这个是用",",分隔ip地址的方法,把整列的ip地址转换成横的一列

===================================
with rs as(
  select 'a' A, 2 B, 'www' C
    from dual
  union all
  select 'b' A, 1 B, 'www' C
    from dual
  union all
  select 'c' A, 3 B, 'sss' C
    from dual
  union all
select 'd' A, 4 B, 'ddd' C from dual)
/*
select rs.C,
  case rs.A when  'a' then rs.B else 0 end ver_high,
  case rs.A when  'b' then rs.B else 0 end v_high,
  case rs.A when  'c' then rs.B else 0 end v_low,
  case rs.A when  'd' then rs.B else 0 end ver_low
   from rs
  */
  select rs.C,
  sum(case rs.A when  'a' then rs.B else 0 end) ver_high,
  sum(case rs.A when  'b' then rs.B else 0 end) v_high,
  sum(case rs.A when  'c' then rs.B else 0 end) v_low,
  sum(case rs.A when  'd' then rs.B else 0 end) ver_low
   from rs
   group by C   from rs
按C分组 显示相应A等级数量