在一个征收信息表中,我要查询当期有纳税额的街道,按照操作机关查处下属街道并排序出来。
select jdxz_dm,jdxz_mc from zsttxx where rkrq between to_date('2006-05-01','yyyy-mm-dd') and to_date('2006-05-31','yyyy-mm-dd') and (zsxm_dm='01' or zsxm_dm='03' or zsxm_dm='04')
然后要根据选定时间,街道代码,分zsxm_dm汇总当期纳税额。
select tt,tt-tq,(tt-tq)/tq (select sum(se) as tt where rkrq between to_date('2006-05-01','yyyy-mm-dd') and to_date('2006-05-31','yyyy-mm-dd') and zsxm_dm='01' and jdxz_dm=XX) a,(select sum(se) as tq where rkrq between to_date('2005-05-01','yyyy-mm-dd') and to_date('2005-05-31','yyyy-mm-dd') and zsxm_dm='01' and jdxz_dm=XX) b
只举了zsxm_dm='01'的汇总纳税额。现在的问题是,后面查询中的两个jdxz_dm=XX地方的XX,要和第一个查询中查出来每行里的一样。现在必须通过第一个查询来确定街道,然后根据街道汇总。而把第一个查询嵌套在XX处,就不出数据。请问高手怎么解决?或者能把两个查询写到一起。后面还要相同的汇总zsxm_dm为03,04,05,的时候的税额。跪求高手解决.

解决方案 »

  1.   

    select a.zsxm_dm, a.jdxz_dm, a.tt, a.tt - b.tq, (a.tt - b.tq) / b.tq
      from (select distinct zsxm_dm, jdxz_dm, sum(se) as tt
              from zsttxx
             where rkrq between to_date('2006-05-01', 'yyyy-mm-dd') and
                   to_date('2006-05-31', 'yyyy-mm-dd')) a,
           (select distinct zsxm_dm, jdxz_dm, sum(se) as tq
              from zsttxx
             where rkrq between to_date('2005-05-01', 'yyyy-mm-dd') and
                   to_date('2005-05-31', 'yyyy-mm-dd')) b
     where a.zsxm_dm = b.zsxm_dm
       and a.jdxz_dm = b.jdxz_dm
      

  2.   

    上面的有点问题
    select a.zsxm_dm, a.jdxz_dm, a.tt, a.tt - b.tq, (a.tt - b.tq) / b.tq
      from (select distinct zsxm_dm, jdxz_dm, sum(se) as tt
              from zsttxx
             where rkrq between to_date('2006-05-01', 'yyyy-mm-dd') and
                   to_date('2006-05-31', 'yyyy-mm-dd')
             group by zsxm_dm, jdxz_dm) a,
           (select distinct zsxm_dm, jdxz_dm, sum(se) as tq
              from zsttxx
             where rkrq between to_date('2005-05-01', 'yyyy-mm-dd') and
                   to_date('2005-05-31', 'yyyy-mm-dd')
             group by zsxm_dm, jdxz_dm) b
     where a.zsxm_dm = b.zsxm_dm
       and a.jdxz_dm = b.jdxz_dm
      

  3.   

    你上面sql有问题,最好给出表结构和测试数据,这样便于理解
      

  4.   

    是有问题,
    表zsttxx
    ZSXM_DM   SE            RKRQ               JDXZ_DM
    01       4232.32        2006-05-06        15114230002
    03       35.25          2005-05-24        15114230002
    04       6565.5         2006-05-12        15114230004
    01       65.35          2006-05-07        15114230002表dm_jdxz
    JDXZ_DM          JDXZ_MC           SWJG_DM
    15114230002      巢湖路           15114230000
    15114230004      水库路           15114230000
    15114220004      愧术路           15114220000由于数据很多,不能全部写,只能写个有代表性和用到的数据第二个查询应该是
    select  DECODE(B.TQ,0,'-',ROUND((A.TT-B.TQ)*100/B.TQ,2)||'%'),decode(b.tq,NULL,0,round((a.tt-b.tq)/10000,2)),round(a.tt/10000,2)  
    from (select sum(se) as tt from zsttxx where rkrq>=to_date('2006-05-01','yyyy-mm-dd') and rkrq<=to_date('2006-05-31','yyyy-mm-dd') and zsxm_dm='01' and  jdxz_dm=&&L2) a,(select nvl(sum(se),0) as tq from zsttxx where add_months(rkrq,12)>=to_date('2005-05-01','yyyy-mm-dd') and add_months(rkrq,12)<=to_date('2005-05-31','yyyy-mm-dd') and zsxm_dm='01' and jdxz_dm=&&L2) b
      

  5.   

    根据上面测试数据,结果数据应该是怎样的?
    怎么你的sql会有第2年的数据add_months(rkrq,12)?
      

  6.   

    应该是
    select  DECODE(B.TQ,0,'-',ROUND((A.TT-B.TQ)*100/B.TQ,2)||'%'),decode(b.tq,NULL,0,round((a.tt-b.tq)/10000,2)),round(a.tt/10000,2)  
    from (select sum(se) as tt from zsttxx where rkrq>=to_date('2006-05-01','yyyy-mm-dd') and rkrq<=to_date('2006-05-31','yyyy-mm-dd') and zsxm_dm='01' and  jdxz_dm=&&L2) a,(select nvl(sum(se),0) as tq from zsttxx where add_months(rkrq,12)>=to_date('2006-05-01','yyyy-mm-dd') and add_months(rkrq,12)<=to_date('2006-05-31','yyyy-mm-dd') and zsxm_dm='01' and jdxz_dm=&&L2) b
    这样是得出去年同期,和今年当期两个数据.因为在实际用的时候,时间是$$0018这样的系统参数.最后应该是
                             增  殖   税                           消   费    税
    JDXZ_DM    JDXZ_MC 当期入库   去年同期   同期增减比  当期入库   去年同期   同期增减比
    15114230002  巢湖路   4297.67     **          **%       35.25     **         **%
    ..........数据情况很多,不能一一列出.
    现在问题是JDXZ_DM  JDXZ_MC不确定,每次要根据选择的税务机关查询下设的街道乡镇,然后根据查出的街道乡镇分税种去查询,得出结果.原来可以用&&L3之类的相对地址取前面查出来的结果,结果表格生成很慢,现在要加快速度,要取消相对地址的使用.但两个查询嵌套好象不托,不出数据.要问怎么解决这个问题.
      

  7.   

    使用union可以把两个查询连接起来,能不能
    select(1) str from table1  union select(2) sdk from table2 where stt=str
    这里第二个查询里面用的stt=str中的str为响应的第一个查询出来的str不知道能不能实现这样的功能??求高手解决。
      

  8.   

    上面是乱想,union只能在下面连接,不能在右边连接结果。能有方法右边连接吗??
      

  9.   

    描述的还不够清楚,以前解答过类似求去年同期数据问题,你可以参考下:
    http://community.csdn.net/Expert/topic/4594/4594448.xml?temp=.7217676