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

oracle查询问题(牛人请进,庸人看热闹也行。欢迎各种类型人士参观)
表A
date            co2
2012-07-01 01    4
2012-07-02 02    3
......直到23点
2012-07-02 01    5
要求达到把每天的数据变成一行,查询每月数据(即把列变行,交叉表)
例如
2012-07-01 01的co2的值     2012-07-01 02的co2的值    ....2012-07-01 23
4                                 3                               //显示7月1号每小时的co2的值
                                                                  //显示7月2号每小时co2的值
现在问题来了我能查询7月1号到4号的4条记录,可是到5号就不行了。sql太长了。
所以请高手写个少点的牛X的。
感谢各位。
给点启发。。思路
------最佳解决方案--------------------
下班回家,帮你顶。
------其他解决方案--------------------
这个可以。

select date,max(col2)
  from
(select date,case substr(col2,-2,2)
             when '01' then col2
             when '02' then col2
             ....
             when '23' then col2
             else 0 end as col2
   from t)
group by date;


引用:
SQL code1234567891011with t as (select to_date('2012-07-01 01','yyyy-mm-dd hh24') col1, 4 col2 from dual union all select to_date('2012-07-01 02','yyyy-mm-dd hh24') col1, 3 col2 from dual……

------其他解决方案--------------------
with test as (
select '2012-10-1' as str1, '11' as str2 from dual
union all
select '2012-10-2' as str1, '12' as str2 from dual
union all
select '2012-10-3' as str1, '13' as str2 from dual
union all
select '2012-10-4' as str1, '14' as str2 from dual
union all
select '2012-10-5' as str1, '15' as str2 from dual
union all
select '2012-11-1' as str1, '12' as str2 from dual
union all
select '2012-11-2' as str1, '22' as str2 from dual
union all
select '2012-11-3' as str1, '32' as str2 from dual
union all
select '2012-11-4' as str1, '42' as str2 from dual
union all
select '2012-11-5' as str1, '52' as s