日期:2014-05-19  浏览次数:20538 次

帮忙!临时表问题
下面的sql,能否不用临时表?


select   XZJD_MC,   B.jfxzc_mc,B.JFXZC_MJ,B.QX_DM,B.zq_dm,B.jfxzc_dm,
(select   sum(tb_mj)   from   ST_DLTB   where   DL_DM= '111 '   and   SZXZC_DM=JFXZC_MC   group   by   JFXZC_MC)as   e111,
(select   sum(tb_mj)   from   ST_DLTB   where   DL_DM= '325 '   and   SZXZC_DM=JFXZC_MC   group   by   JFXZC_MC)as   cw325
into   #ccc
from   st_xzorjd   as   A
inner   join   st_jforxzc   B   on   SUBSTRING(A.XZJD_DM,7,3)=B.zq_dm

select   xzjd_mc,sum(jfxzc_mj)as   jfxzc_mj,sum(e111)   as   e111,sum(cw325)   as   cw325
from   #ccc
group   by   xzjd_mc

drop   table   #ccc


非常感谢

------解决方案--------------------
可以

select xzjd_mc,sum(jfxzc_mj)as jfxzc_mj,sum(e111) as e111,sum(cw325) as cw325
from
(
select XZJD_MC, B.jfxzc_mc,B.JFXZC_MJ,B.QX_DM,B.zq_dm,B.jfxzc_dm,
(select sum(tb_mj) from ST_DLTB where DL_DM= '111 ' and SZXZC_DM=JFXZC_MC group by JFXZC_MC)as e111,
(select sum(tb_mj) from ST_DLTB where DL_DM= '325 ' and SZXZC_DM=JFXZC_MC group by JFXZC_MC)as cw325
from st_xzorjd as A
inner join st_jforxzc B on SUBSTRING(A.XZJD_DM,7,3)=B.zq_dm
) B
group by xzjd_mc
------解决方案--------------------
select xzjd_mc,sum(jfxzc_mj)as jfxzc_mj,sum(e111) as e111,sum(cw325) as cw325 from
(
select XZJD_MC, B.jfxzc_mc,B.JFXZC_MJ,B.QX_DM,B.zq_dm,B.jfxzc_dm,
(select sum(tb_mj) from ST_DLTB where DL_DM= '111 ' and SZXZC_DM=JFXZC_MC group by JFXZC_MC)as e111,
(select sum(tb_mj) from ST_DLTB where DL_DM= '325 ' and SZXZC_DM=JFXZC_MC group by JFXZC_MC)as cw325
from st_xzorjd as A
inner join st_jforxzc B on SUBSTRING(A.XZJD_DM,7,3)=B.zq_dm
) t
group by xzjd_mc

------解决方案--------------------
select XZJD_MC, sum(B.jfxzc_mc),sum(B.JFXZC_MJ),sum(B.QX_DM),sum(B.zq_dm),sum(B.jfxzc_dm),
(select sum(tb_mj) from ST_DLTB where DL_DM= '111 ' and SZXZC_DM=JFXZC_MC group by JFXZC_MC)as e111,
(select sum(tb_mj) from ST_DLTB where DL_DM= '325 ' and SZXZC_DM=JFXZC_MC group by JFXZC_MC)as cw325
--into #ccc
from st_xzorjd as A
inner join st_jforxzc B on SUBSTRING(A.XZJD_DM,7,3)=B.zq_dm
group by xzjd_mc