[code=SQL]
 select distinct(daima),decode(daima,'1','上海青','2','白萝卜','3','小白菜','4','西红柿','5','土豆','6','黄瓜','7','薄皮青椒','8','茄子','9','四季豆','10','芹菜','11','包菜','12','洋葱','13','冬瓜','14','花菜','15','大白菜','16','生菜','17','胡萝卜','18','藕','') as cm,
(select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd01'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field1,
(select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd01'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field2,
(select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd02'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field3,
(select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd02'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field4,
(select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd03'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field5,
(select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd03'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field6,
(select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd04'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field7,
(select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd04'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field8,
(select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd05'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field9,
(select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd05'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field10,
(select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd06'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field11,
(select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd06'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field12,
(select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd07'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field13,
(select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd07'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field14,
(select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd08'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field15,
(select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd08'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field16,
(select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd09'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field17,
(select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd09'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field18
 from dyna_dataset_nfcp_range1 f ORDER BY to_number(daima)
[/code怎么优化啊,查询耗时13秒多

解决方案 »

  1.   


     select distinct(daima),decode(daima,'1','上海青','2','白萝卜','3','小白菜','4','西红柿','5','土豆','6','黄瓜','7','薄皮青椒','8','茄子','9','四季豆','10','芹菜','11','包菜','12','洋葱','13','冬瓜','14','花菜','15','大白菜','16','生菜','17','胡萝卜','18','藕','') as cm,
    (select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd01'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field1,
    (select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd01'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field2,
    (select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd02'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field3,
    (select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd02'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field4,
    (select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd03'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field5,
    (select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd03'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field6,
    (select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd04'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field7,
    (select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd04'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field8,
    (select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd05'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field9,
    (select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd05'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field10,
    (select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd06'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field11,
    (select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd06'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field12,
    (select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd07'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field13,
    (select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd07'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field14,
    (select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd08'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field15,
    (select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd08'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field16,
    (select sum(to_number(field1)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd09'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field17,
    (select sum(to_number(field2)) from dyna_dataset_nfcp_range1 r where r.reportinstance_guid in(select i.reportinstance_guid from rpt7_reportinstance i where i.template_guid='AC100142FFFFFFFF8E6F8CAF00001075' and i.verifystatus>=0 and i.so_instance_code='pjcd09'and i.reportperiod_guid in (select distinct(d.reportperiod_guid) from Rpt7_ReportPeriod d where substr(d.reportperiod_code,5,7) ='2012-01')) and r.daima=f.daima ) as field18
     from dyna_dataset_nfcp_range1 f ORDER BY to_number(daima)
      

  2.   

    表dyna_dataset_nfcp_range1 
    daima是列名
    field1是价格 field2是销量
    pjcd01pjcd02pjcd03pjcd04.是用户
    erifystatus>=0是上报审核通过或者未通过的条件
      

  3.   

    如果都是来至一张表,可以用case when 来弄 。
      

  4.   

    看完楼主的这个sql,吐血三公升... 而亡,
    这也忒长了
      

  5.   

    --试试这个
    select r.daima as vName, --蔬菜名称
           /*r.daima的decode,*/
           sum(case
                 when i.so_instance_code = 'pjcd01' then
                  field1
                 else
                  0
               end) as F_pjcd01_price,
           sum(case
                 when i.so_instance_code = 'pjcd01' then
                  field2
                 else
                  0
               end) as F_pjcd01_sale,
           '...pjcd02' /*后面的用户请自已补充*/
      from dyna_dataset_nfcp_range1 r, rpt7_reportinstance i
     where r.reportinstance_guid = i.reportinstance_guid
       and i.template_guid = 'AC100142FFFFFFFF8E6F8CAF00001075'
       and i.verifystatus >= 0
       and exists
     (select 1 --判断2012-01是否产生数据
              from Rpt7_ReportPeriod d
             where i.reportperiod_guid = d.reportperiod_guid
               and substr(d.reportperiod_code, 5, 7) = '2012-01')
     group by r.daima, i.so_instance_code
      

  6.   

    ORA-00932:数据类型不一致,应为char,但却获得的number