我数据库表结构:rpt_http_code:ID        CODE         DATE
-----------------------------------
1          505          2011-04-12
2          404          2011-04-12
3          500          2011-04-12我需要统计不同CODE出现的次数,505和500属于同一类,命名类名为5XX。其他的CODE也是这样分我通过sql查询出的结果为:
sql语句:
select d, code, count(d) from (select date as d, substr(code, 1, 1) as code from rpt_http_code) t group by code,d===
DATE               CODE           COUNT
----------------------------------------------
2011-04-12          5             2
2011-04-12          4             1
但是这还不符合我的要求:
我希望结果是这样:
DATE                 5       4
---------------------------------
2011-04-12           2       1
不知道是否能在前面的查询结果之上再进行‘行转列’,或者大家有更直接的办法最好是能把字段名5和4改成:5XX和4XX这2个分类的名字。   麻烦站类各位大哥解决下,希望给出具体代码  谢谢

解决方案 »

  1.   

    select date,
           sum(case substr(code, 1, 1) when '5' then 1 else 0 end) "5",
           sum(case substr(code, 1, 1) when '4' then 1 else 0 end) "4"
    from rpt_http_code
    group by date
      

  2.   

    select date,
           sum(case substr(code, 1, 1) when '5' then 1 else 0 end) "5",
           sum(case substr(code, 1, 1) when '4' then 1 else 0 end) "4"
    from rpt_http_code
    group by date
      

  3.   

    select date,
           sum(case substr(code, 1, 1) when '5' then 1 else 0 end) "5XX",
           sum(case substr(code, 1, 1) when '4' then 1 else 0 end) "4XX"
    from rpt_http_code
    group by date