select 账套名称,
cast(年度 as varchar(10))+'年' as 年度,
case when 期间<10 then '0'+cast(期间 as varchar(10))+'月' else cast(期间 as varchar(10))+'月' end as 期间,
case when 期间<10 then cast(年度 as varchar(10))+'年'+'0'+cast(期间 as varchar(10))+'月' else cast(年度 as varchar(10))+'年'+cast(期间 as varchar(10))+'月' end as 年度期间,
'费用' as 指标分类,一级科目名称,二级科目名称,
isnull((select sum(本期本币金额) from FA_Value_Sunyi_jiegou as ff where 一级科目名称 in ('销售费用','管理费用','财务费用')
and 是否包含未记账='$(财务分析_是否包含未记账)'
and ff.账套名称=t.账套名称 and cast(ff.年度 as int)=t.年度 and cast(ff.期间 as int)=t.期间 and ff.一级科目名称=t.一级科目名称 and ff.二级科目名称=t.二级科目名称),0) as 本期金额
from
(
select distinct 账套名称,cast(年度 as int) as 年度,cast(期间 as int) as 期间,一级科目名 称, 二级科目名称
from FA_Value_Sunyi_jiegou
where 一级科目名称 in ('销售费用','管理费用','财务费用')
and 账套名称 in ($(财务分析_账套名称))
and cast(年度 as int)=left('$(日期-年月)',4)
and cast(期间 as int)<=right('$(日期-年月)',2)
)t