美文网首页
SQL做K3资金滚动报表

SQL做K3资金滚动报表

作者: 刘刘琼Max | 来源:发表于2020-04-09 21:00 被阅读0次

本人产品,sql小白。

在同事网友指导下,做出一点小模样。晒晒,求交流更好方案。

财务需求如下表:——资金滚动月报

image

财务想做通过选择一个日期,累计至所选日期统计出收入、支出的不同用途下的实际收付款人民币金额数据。
【难点】

1、收支分类如何维护?——在金蝶K3中 收款读取收付款用途,支出读取费用项目,同时二级标题中加入数字来做排序。
2、外币统一转化成人民币?——读取每个月的汇率,计算出人民币金额

--汇率临时表
select X.FBEGDATE as BDATE,X.FCYFORID as YB, X.FEXCHANGERATE as ZJHL into #HL
from T_BD_Rate X
where convert(char(4), X.FBEGDATE, 120) = convert(char(4), '#BDate#', 120)
and X.FRATETYPEID = '8355919'
 
 
--收款临时表
 
left join #HL
on SK.FCURRENCYID = #HL.YB
and  convert(char(7),#HL.BDATE, 120) = convert(char(7), SK.FDATE, 120),

3、根据所选日期,当月1日是周三时,本周统计>本月统计,如何解决?

--本周收款
select (case when sum(A5) is not null then sum(A5) else '0' end ) as BWLJ
into #SBWLJ
from #SKSUM
where convert(char(10), A1, 120) < dateadd(day, 1, '#BDate#')     --小于查询日期范围 
and datepart(mm,A1) = datepart(mm,'#BDate#')    --是当前月
and datepart(WW,A1) = datepart(WW, '#BDate#')   --开始查周

表结构:

image.png

解决方案:
搭建:采用临时表存结果数据,最后完成组装。

--定义变量
DECLARE @BDate Datetime; --业务日期
SET @BDate= '#BDate#';

--汇率临时表 #HL
select X.FCYFORID as YB, X.FEXCHANGERATE as ZJHL into #HL
from T_BD_Rate X
where convert(char(7), X.FBEGDATE, 120) = convert(char(7), @BDate, 120)
and X.FRATETYPEID = '8355919'

--收付款用途临时表 #YT
select
 JCZL.FDESCRIPTION1  as A2 
,JCZL.FDESCRIPTION as A3
,JCZL.FNAME as A4
into #YT
from T_CN_RECPAYPURPOSE_L as JCZL
where JCZL.FLOCALEID = '2052'
and JCZL.FNAME not Like '%调拨%'
order by A4

--费用项目id临时表 #FYMX
select a.FEXPID A1 
,c.FNAME A2 --一级收支类别:经营性/非经营
,a.FDESCRIPTION A3 --二级收支类别:业务收入等
,a.FNAME A4    --费用项目明细
into  #FYMX
from T_BD_EXPENSE_L as a,T_BD_EXPENSE as b,T_BD_EXPENSE_GROUP_L as c
where a.FEXPID=b.FEXPID
--and b.FGROUP = c.FID
and a.FLOCALEID = '2052'

--收款临时表 #SKSUM
select
--SK.FBILLNO as A0,
SK.FDATE as A1 
,JCZL.FDESCRIPTION1  as A2 
,JCZL.FDESCRIPTION as A3
,JCZL.FNAME as A4
      
,(case when SK.FCURRENCYID = '1' 
then  
     SKMX.FREALRECAMOUNTFOR  
else 
     SKMX.FREALRECAMOUNTFOR * #HL.ZJHL
end) as A5

into #SKSUM
from T_AR_RECEIVEBILL as SK
    left join #HL on SK.FCURRENCYID = #HL.YB,

T_AR_RECEIVEBILLENTRY as SKMX, 
T_CN_RECPAYPURPOSE_L as JCZL

where SK.FID = SKMX.FID
and SKMX.FPURPOSEID = JCZL.FID
and JCZL.FLOCALEID = '2052'
and JCZL.FNAME not Like '%调拨%'
and SK.FDOCUMENTSTATUS = 'C'
and convert(char(10), A1, 120) < dateadd(day, 1, @BDate)

--支出明细临时表
select  
FK.FDATE as A1 --业务日期
--,#FYMX.A2 as A2  --一级收支类别:经营性/非经营
--,#FYMX.A3 as A3  --二级收支类别:业务收入等
,#FYMX.A4 as A4  --费用项目明细

,(case when FK.FCURRENCYID = '1'
then
    FKMX.FREALPAYAMOUNTFOR
else
    FKMX.FREALPAYAMOUNTFOR * #HL.ZJHL
end) as A5

into #FKSUM
from T_AP_PAYBILL as FK
left join #HL on FK.FCURRENCYID = #HL.YB
,T_AP_PAYBILLENTRY as FKMX
left join #FYMX on FKMX.FCOSTID = #FYMX.A1
where  FK.FID = FKMX.FID
and FK.FDOCUMENTSTATUS = 'C'
and FKMX.FCOSTID <>''
and #FYMX.A4 <>''

2、开始组装

--收款组装起来
select TOP 100 PERCENT '收款' as A1 
,A2
--,A3
,A4
,sum(BDLJ) '本日累计'
,sum(BWLJ) '本周累计'
,sum(BMLJ) '本月累计'
,sum(BYLJ) '本年累计'
,sum(LJ01) '1月'
,sum(LJ02) '2月'
,sum(LJ03) '3月'
,sum(LJ04) '4月'
,sum(LJ05) '5月'
,sum(LJ06) '6月'
,sum(LJ07) '7月'
,sum(LJ08) '8月'
,sum(LJ09) '9月'
,sum(LJ10) '10月'
,sum(LJ11) '11月'
,sum(LJ12) '12月'

  from (

SELECT '收款' as A1 
,#YT.A2 as A2
--,#YT.A3 as A3
,#YT.A4 as A4
,(case when BDLJ is not null then BDLJ else '0' end ) as BDLJ  
,(case when BWLJ is not null then BWLJ else '0' end ) as BWLJ 
,(case when BMLJ is not null then BMLJ else '0' end ) as BMLJ 
,(case when BYLJ is not null then BYLJ else '0' end ) as BYLJ
,(case when LJ01 is not null then LJ01 else '0' end ) as LJ01 
,(case when LJ02 is not null then LJ02 else '0' end ) as LJ02
,(case when LJ03 is not null then LJ03 else '0' end ) as LJ03
,(case when LJ04 is not null then LJ04 else '0' end ) as LJ04 
,(case when LJ05 is not null then LJ05 else '0' end ) as LJ05
,(case when LJ06 is not null then LJ06 else '0' end ) as LJ06
,(case when LJ07 is not null then LJ07 else '0' end ) as LJ07
,(case when LJ08 is not null then LJ08 else '0' end ) as LJ08 
,(case when LJ09 is not null then LJ09 else '0' end ) as LJ09 
,(case when LJ10 is not null then LJ10 else '0' end ) as LJ10
,(case when LJ11 is not null then LJ11 else '0' end ) as LJ11
,(case when LJ12 is not null then LJ12 else '0' end ) as LJ12 

FROM #YT
LEFT JOIN 
(
--本日收款
select A4,sum(A5) as BDLJ
from #SKSUM 
where convert(char(10), A1, 120) = @BDate
group by A4
) as BDLJ
 ON #YT.A4 = BDLJ.A4
--后面就是重复

union all

--支出表

union all
--总收支

最后,由于采用太多临时表,
在金蝶K3 BOS平台上不能写删除语句,反复运行过多时会报错,致电金蝶说建议采用存储过程写。于是目前在研究中。

相关文章

  • SQL做K3资金滚动报表

    本人产品,sql小白。 在同事网友指导下,做出一点小模样。晒晒,求交流更好方案。 财务需求如下表:——资金滚动月报...

  • SQL基础

    SQL简介 SQL基本语句 1、增加一条记录 insert into table_name(k1,k2,k3) V...

  • 报表工具有没有sql植入风险?如何规避?

    报表检测到sql植入风险,一般是报表工具提供了通用查询的功能,也就意味着sql(sql类数据集)是可以通过参数动态...

  • SQL Server报表生成器入门

    最近几周都在客户公司做项目,偶然的机会看到他们的主管在教怎么使用SQL Server报表生成器快速创建所需报表。感...

  • 浅谈财务报表粉饰之货币资金

    货币资金作为时点数,是财务报表中最容易发生资金造假的科目,通常很多企业在粉饰财务报表时,货币资金也是“重灾区”。现...

  • 资金日报表

    参与即学习,分享即成长。 今天学习资金版块第一讲:资金日报表,公司的第一张管理报表,编制的意义是啥,为啥要编制、如...

  • 封装javascript条件查询表格

    用过5年多的金碟k3,最近一个月在自己弄财务软件,觉得k3的过滤框挺好,但是没用过sql的人不一定会用得好,于是去...

  • 感情乙方就是背锅的?

    今天加班,一个报表的问题处理了一天,不得不说用SQL做报表是真的恶心。因为一个数据改了几天都没改对,客户对我的工作...

  • 数据分析师 - Week15

    SAS技术 用tabulate制作数据透视表 用sql制作报表

  • 皕杰报表中的参数及其作用 2022-07-28

    为了给报表中已知数据类型的信息进行赋值,皕杰报表设置了参数。 我们可以通过参数给报表传递有用的信息,来控制sql语...

网友评论

      本文标题:SQL做K3资金滚动报表

      本文链接:https://www.haomeiwen.com/subject/qxdcmhtx.html