表1:
槽号   出铝量   日期
101    1800      2005-1-1
102    1700      2005-1-1
103    1800      2005-1-1
得到如下结果(统计个数):
            【1780,+】    【1750,1780】    【1650,1750】    【-,1650】
日期      一区  二区  三区   一区  二区  三区  一区  二区  三区  一区  二区  三区
2005-1-1    2    0    0       0     0     0     1     0     0      0    0     0  
槽号以一开头的就是一区,依次类推,301就是三区的槽号。
谢谢各位了

解决方案 »

  1.   

    总共槽号有400多个,用的是oracle数据库
      

  2.   

    select a.日期
          ,nvl(sum(decode(b.区号,1,b.出铝量,0)),0) 一区 
          ,nvl(sum(decode(b.区号,2,b.出铝量,0)),0) 二区 
          ,nvl(sum(decode(b.区号,3,b.出铝量,0)),0) 三区 
          ,nvl(sum(decode(c.区号,1,b.出铝量,0)),0) 一区 
          ,nvl(sum(decode(c.区号,2,b.出铝量,0)),0) 二区 
          ,nvl(sum(decode(c.区号,3,b.出铝量,0)),0) 三区 
          ,nvl(sum(decode(d.区号,1,b.出铝量,0)),0) 一区 
          ,nvl(sum(decode(d.区号,2,b.出铝量,0)),0) 二区 
          ,nvl(sum(decode(d.区号,3,b.出铝量,0)),0) 三区 
          ,nvl(sum(decode(e.区号,1,b.出铝量,0)),0) 一区 
          ,nvl(sum(decode(e.区号,2,b.出铝量,0)),0) 二区 
          ,nvl(sum(decode(e.区号,3,b.出铝量,0)),0) 三区 
      from (select distinct 日期 from Table_Name) a
          ,(select substr(槽号,1,1) 区号,出铝量,日期 from table_name where 出铝量>1780) b
          ,(select substr(槽号,1,1) 区号,出铝量,日期 from table_name where 出铝量>1750 and 出铝量<=1780) c
          ,(select substr(槽号,1,1) 区号,出铝量,日期 from table_name where 出铝量>1650 and 出铝量<=1750) d
          ,(select substr(槽号,1,1) 区号,出铝量,日期 from table_name where 出铝量<=1650) e
     where a.日期=b.日期(+)
       and a.日期=c.日期(+)
       and a.日期=d.日期(+)
       and a.日期=e.日期(+)
     group by a.日期
      

  3.   

    更正一下,写错了,不是汇总出铝量,而是统计个数
    select a.日期
          ,nvl(sum(decode(b.区号,1,1,0)),0) 一区 
          ,nvl(sum(decode(b.区号,2,1,0)),0) 二区 
          ,nvl(sum(decode(b.区号,3,1,0)),0) 三区 
          ,nvl(sum(decode(c.区号,1,1,0)),0) 一区 
          ,nvl(sum(decode(c.区号,2,1,0)),0) 二区 
          ,nvl(sum(decode(c.区号,3,1,0)),0) 三区 
          ,nvl(sum(decode(d.区号,1,1,0)),0) 一区 
          ,nvl(sum(decode(d.区号,2,1,0)),0) 二区 
          ,nvl(sum(decode(d.区号,3,1,0)),0) 三区 
          ,nvl(sum(decode(e.区号,1,1,0)),0) 一区 
          ,nvl(sum(decode(e.区号,2,1,0)),0) 二区 
          ,nvl(sum(decode(e.区号,3,1,0)),0) 三区 
      from (select distinct 日期 from Table_Name) a
          ,(select substr(槽号,1,1) 区号,出铝量,日期 from table_name where 出铝量>1780) b
          ,(select substr(槽号,1,1) 区号,出铝量,日期 from table_name where 出铝量>1750 and 出铝量<=1780) c
          ,(select substr(槽号,1,1) 区号,出铝量,日期 from table_name where 出铝量>1650 and 出铝量<=1750) d
          ,(select substr(槽号,1,1) 区号,出铝量,日期 from table_name where 出铝量<=1650) e
     where a.日期=b.日期(+)
       and a.日期=c.日期(+)
       and a.日期=d.日期(+)
       and a.日期=e.日期(+)
     group by a.日期