select a.test1,sum( 1 ) as test1_numfrom table_07 as a,table2 as b,table3 as cwhere a.test1 = b.test1 and b.test2 = c.test2 and a.test4 ='202001' group by a.test1

解决方案 »

  1.   

    select a.test1,sum( 1 ) as test1_numfrom table_07 as a
    Where Exists(select 1 from table2  b where b.test1 = a.test1  and exists(Select 1 FROM table3 C where b.test2 = c.test2)) and a.test4 ='202001' group by a.test1
      

  2.   

    --建立索引
    create index index_test1 on table_07(test1)
    create index index_test1 on table2(test1)
    create index index_test2 on table2(test2)
    create index index_test2 on table3(test2)
      

  3.   

    --给table_07建立聚集索引.如下:create clustered index ind_cl_table_07
            on table_07(test1)--如果其它两个表非常小的话,那就没有必要建立聚集索引,如果有点大的话,那也应该建立聚集索引.
      

  4.   

    1.先对A表建立索引(test1),
    select a.test1,
    COUNT(TEST4)
    from table_07 as a 
    inner join table2 as b on a.test1 = b.test1
    INNER JOIN table3 as c ON b.test2 = c.test2
    WHERE a.test4 ='202001' 
    group by a.test1
      

  5.   

    如果只是这样子我需要问吗?
    我的SQL是这样子的select a.dev_code,c.class_name,b.spare_str2,b.dev_bank,b.dev_name,nvl(sum(case  when a.tr_code in('203001','205051') 
    then 1 end),0) as draw_num,nvl(sum(case when a.tr_code in('203001','205051') 
    then a.amount end),0) as draw_amt,nvl(sum(case  when a.tr_code in('203020','203030','205052') 
    then 1 end),0) as dep_num,nvl(sum(case  when a.tr_code in('203020','203030','205052') 
    then a.amount end),0) as dep_amt,nvl(sum(case  when a.tr_code 
    in('207003','207004','302148','207010','204001','05021') 
    then 1 end),0) as trf_num,nvl(sum(case  when a.tr_code 
    in('207003','207004','302148','207010','204001','05021') 
    then a.amount end),0) as trf_amt,nvl(sum(case when a.tr_code in('202001','205011') 
    then 1 end),0) as Inq_num,nvl(sum(case when a.tr_code in('201102') 
    then 1 end),0) as chgpw_num,nvl(sum(case when a.tr_code in('206002','206001') 
    then 1 end),0) as comm_num,nvl(sum(case when a.tr_code in('206002','206001') 
    then a.amount end),0) as comm_amt,nvl(sum(case when a.tr_code in('204021') 
    then 1 end),0) as rem_num,nvl(sum(case when a.tr_code in('204021') 
    then a.amount end),0) as rem_amt,nvl(sum(case when a.tr_code in('205001') 
    then 1 end),0) as bankbook_num,nvl(sum(case when a.tr_code not in('203001','205051','203020','203030','205052'
     ,'207003','207004','302148','207010','204001','05021',
     '202001','205011','201102','206002','206001','204021','205001'
     ) then 1 end ),0) as oth_tranfrom Journal_07 as a,dev_bmsg as b,dev_class_list as cwhere a.dev_code = b.dev_code and b.dev_class = c.dev_classgroup by a.dev_code,c.class_name,b.spare_str2,b.dev_bank,b.dev_nameorder by a.dev_code,b.dev_bank七百万数据,一个小时都没有统计出来~dev_code已经建了索引
      

  6.   

    select a.test1,sum(case  when a.test4 ='202001' 
    then 1 end) as test1_numfrom table_07 as a,table2 as b,table3 as cwhere b.test1 = a.test1 and b.test2 = c.test2group by b.test1