select * from (
select cwzz,
jt,
zz,
jd,
case when jd ='第一季度' then '1'
when jd ='第二季度' then '2'
when jd ='第三季度' then '3'
when jd ='第四季度' then '4'
end xh, --将季度转换为数字表示
fl,
case when fl='房产税合计:税金及附加-房产税' then round((sum(jffy)/10000),2)
when fl='财务费用' then round((sum(jffy)/10000),2)
when fl='餐饮部支出:养老成本-食材费用' then round((sum(jffy)/10000),2)
when fl='水电气:水费、电费、燃气' then round((sum(jffy)/10000),2)
end je,--不同的对分类进行数据处理
nd
from(
select cwzz,
jt,
zz,
fl,--分类
nd,--年度
jffy,
case when qj in ('01','02','03') then '第一季度'
when qj in ('04','05','06') then '第二季度'
when qj in ('07','08','09') then '第三季度'
when qj in ('10','11','12') then '第四季度'
end jd --将时间划分为季度
from(
select a.pk_group as jt,
a.pk_org as zz,
a.periodv as qj,
a.explanation,
case when a.accountcode ='640302' then '房产税合计:税金及附加-房产税'
when a.accountcode in ('640202','660111','660211','640203','660112','660212','640204','660113','660213','660114','660214') then '水电气:水费、电费、燃气'
when a.accountcode like '6603%' then '财务费用'
when a.accountcode ='500306' then '餐饮部支出:养老成本-食材费用'
end fl,--根据会计科目进行分类划分
a.debitamount as jffy,
d.name as cwzz,
a.yearv as nd,
a.accountcode
from gl_detail a,org_accountingbook b,org_group c,org_corp d
where d.pk_corp=a.pk_org
and b.pk_accountingbook =a.pk_accountingbook
and c.pk_group=a.pk_group
and a.explanation not in('期初')--排除期初数据
and a.dr=0 and d.name =('江苏澳洋优居壹佰养老产业有限公司'))aa--限定优居壹佰
)bb
where fl in('房产税合计:税金及附加-房产税','水电气:水费、电费、燃气','财务费用','餐饮部支出:养老成本-食材费用')
group by cwzz, jt, zz,jd, nd, fl
union all
select '合并' cwzz,
'0001V110000000002247' jt,
'' zz,
jd,
case when jd ='第一季度' then '1'
when jd ='第二季度' then '2'
when jd ='第三季度' then '3'
when jd ='第四季度' then '4'
end xh,
fl,
sum(je) je,
nd
from(
select
jd,
fl,
case when fl='养老公寓折旧合计:累计折旧' then round((sum(dffy)/10000),2)
when fl='人力成本合计:养老成本/护理成本-人力成本【工资,社保,公积金,福利费】' then round((sum(dffy)/10000),2)
end je,
nd
from(
select cwzz,
jt,
zz,
fl,
nd,
dffy,
case when qj in ('01','02','03') then '第一季度'
when qj in ('04','05','06') then '第二季度'
when qj in ('07','08','09') then '第三季度'
when qj in ('10','11','12') then '第四季度'
end jd
from(
select a.pk_group as jt,
a.pk_org as zz,
a.periodv as qj,
a.explanation,
case when a.accountcode in ('160201','160202','160203','160204','160205','160206','160207','160208') then '养老公寓折旧合计:累计折旧'
when a.accountcode in ('2211','221101','22110101','22110102','22110103','22110104','22110105','221102','221103','22110301','22110302','22110303','22110304','22110305','22110306','221104','221105','221106','221107','221108') then '人力成本合计:养老成本/护理成本-人力成本【工资,社保,公积金,福利费】'
end fl, --根据会计科目区分分类
a.creditamount as dffy,
d.name as cwzz,
a.yearv as nd,
a.accountcode
from gl_detail a,org_accountingbook b,org_group c,org_corp d
where d.pk_corp=a.pk_org
and b.pk_accountingbook =a.pk_accountingbook
and c.pk_group=a.pk_group
and a.explanation not in('期初')--去除期初数据
and a.dr=0 and d.name in ('江苏澳洋优居壹佰养老产业有限公司','南通市阳光澳洋护理院有限公司','张家港优居壹佰护理院有限公司'))
cc)dd
where fl in('养老公寓折旧合计:累计折旧','人力成本合计:养老成本/护理成本-人力成本【工资,社保,公积金,福利费】')
group by cwzz, jt, zz, jd, fl, nd)
group by jd, fl, '合并', '', '0001V110000000002247', nd
) m
where 1=1 ${if(len(年度)=0,"","AND m.nd in('"+年度+"')")}
and
1=1 ${if(len(会计期间)=0,"","AND m.jd in ('"+会计期间+"')")}
order by m.xh