日期:2014-05-18  浏览次数:20568 次

树形数据汇总查询 - -
SQL code

--SQL2000:
--查询的数据语句:
SELECT DISTINCT B.KJND,B.GSDM,A.FZDM,A.FZMC,
ZBZE1=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN ZBZE1 ELSE 0 END),
ZBZE2=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN ZBZE2 ELSE 0 END),
ZBZE3=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN ZBZE3 ELSE 0 END),
--ZBZE4=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN ZBZE2 ELSE 0 END),--z指标来源没确定
JP1=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN JP1 ELSE 0 END),
JP2=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN JP2 ELSE 0 END),
JP3=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN JP3 ELSE 0 END),
LH1=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN LH1 ELSE 0 END),
LH2=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN LH2 ELSE 0 END),
LH3=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN LH3 ELSE 0 END),
CH1=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN CH1 ELSE 0 END),
CH2=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN CH2 ELSE 0 END),
CH3=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN CH3 ELSE 0 END),
HJ1=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN HJ1 ELSE 0 END),
HJ2=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN HJ2 ELSE 0 END),
HJ3=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN HJ3 ELSE 0 END),
CY1=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN CY1 ELSE 0 END),
CY2=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN CY2 ELSE 0 END),
CY3=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN CY3 ELSE 0 END),
CN1=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN CN1 ELSE 0 END),
CN2=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN CN2 ELSE 0 END),
CN3=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN CN3 ELSE 0 END),
NA1=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN NA1 ELSE 0 END),
NA2=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN NA2 ELSE 0 END),
NA3=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN NA3 ELSE 0 END),
ZS1=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN ZS1 ELSE 0 END),
ZS2=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN ZS2 ELSE 0 END),
ZS3=SUM(CASE WHEN A.FZDM=B.YSKMDM THEN ZS3 ELSE 0 END)
FROM
(select distinct fzdm,case 
when len(fzdm)=3 then fzmc 
when len(fzdm)=5 then '  '+fzmc
when len(fzdm)=7 then '    '+fzmc end 
fzmc from gl_fzxzl WHERE FZDM LIKE '2%') AS A,
(Select 
    KJND,
    GSDM,
    YSKMDM,
    ZBZE1=SUM(CASE WHEN (ZBLYDM like '0601%'or ZBLYDM like '0501%' or ZBLYDM like '0401%') THEN JE ELSE 0 END), --没有合计ZBZE4的金额
    ZBZE2=SUM(CASE WHEN (ZBLYDM like '0601%') THEN JE ELSE 0 end),
    ZBZE3=SUM(CASE WHEN (ZBLYDM like '0401%'or ZBLYDM like '0501%') THEN JE ELSE 0 end),
    --ZBZE4=SUM(CASE WHEN (ZBLYDM like --未确定') THEN JE ELSE 0 end),--没有确定指标来源
    JP1=SUM(CASE WHEN (ZBLYDM like '0601%'or ZBLYDM like '0501%' or ZBLYDM like '0401%') AND YSDWDM='117001' THEN JE ELSE 0 END),
    JP2=SUM(CASE WHEN (ZBLYDM like '0601%') AND YSDWDM='117001' THEN JE ELSE 0 end),
    JP3=SUM(CASE WHEN (ZBLYDM like '0401%'or ZBLYDM like '0501%') AND YSDWDM='117001' THEN JE ELSE 0 end),
    LH1=SUM(CASE WHEN (ZBLYDM like '0601%'or ZBLYDM like '0501%' or ZBLYDM like '0401%') AND YSDWDM='117002' THEN JE ELSE 0 END),
    LH2=SUM(CASE WHEN (ZBLYDM like '0601%') AND YSDWDM='117002' THEN JE ELSE 0 end),
    LH3=SUM(CASE WHEN (ZBLYDM like '0401%'or ZBLYDM like '0501%') AND YSDWDM='117002' THEN JE ELSE 0 end),
    CH1=SUM(CASE WHEN (ZBLYDM like '0601%'or ZBLYDM like '0501%' or ZBLYDM like '0401%') AND YSDWDM='117004' THEN JE ELSE 0 END),
    CH2=SUM(CASE WHEN (ZBLYDM like '0601%') AND YSDWDM='117004' THEN JE ELSE 0 end),
    CH3=SUM(CASE WHEN (ZBLYDM like '0401%'or ZBLYDM like '0501%') AND YSDWDM='117004' THEN JE ELSE 0 end),
    HJ1=SUM(CASE WHEN (ZBLYDM like '0601%'or ZBLYDM like '0501%' or ZBLYDM like '0401%') AND YSDWDM='117003' THEN JE ELSE 0 END),
    HJ2=SUM(CASE WHEN (ZBLYDM like '0601%') AND YSDWDM='117003' THEN JE ELSE 0 end),
    HJ3=SUM(CASE WHEN (ZBLYDM like '0401%'or ZBLYDM like '0501%') AND YSDWDM='117003' THEN JE ELSE 0 end),
    CY1=SUM(CASE WHEN (ZBLYDM like '0601%'or ZBLYDM like '0501%' or ZBLYDM like '0401%') AND YSDWDM='117005' THEN JE ELSE 0 END),
    CY2=SUM(CASE WHEN (ZBLYDM like '0601%') AND YSDWDM='117005' THEN JE ELSE 0 end),
    CY3=SUM(CASE WHEN (ZBLYDM like '0401%'or ZBLYDM like '0501%') AND YSDWDM='117005' THEN JE ELSE