简体   繁体   English

动态 T-SQL pivot 查询资产负债表报表

[英]Dynamic T-SQL pivot query for balance sheet report

I have been tasked to build a balance sheet report, i have built the pivot query below but I am finding some challenges:我的任务是建立资产负债表报告,我在下面建立了 pivot 查询,但我发现了一些挑战:

1 - the report should run based on the variable date, which means for example the column [2021-01-31] should bring all historical data until 31/01/2021, and the following column [2021-02-28] should bring all historical data until 28/02/2021, currently I can only populate the second column 1 - 报告应该基于可变日期运行,这意味着例如列 [2021-01-31] 应该带来所有历史数据,直到 31/01/2021,而以下列 [2021-02-28] 应该带来直到 2021 年 2 月 28 日的所有历史数据,目前我只能填充第二列

2 - Date columns should be dynamic so if in march the column [2021-03-31] would be added automatically 2 - 日期列应该是动态的,所以如果在 3 月份,列 [2021-03-31] 将被自动添加

Is there any way of achieving what I am asking?有什么方法可以实现我的要求吗?

Warm Regards, Daniel热烈的问候,丹尼尔

DECLARE 

@to_date DATETIME


SET @to_date = (select eomonth( getdate()))



select "Classification","Account Name",[2021-01-31],[2021-02-28]

from
(

        select
        'Assets' as "Group Mask"

        ,case when t0.FatherNum = 'A20' then 'AR' 
        when t0.FatherNum in ('A40','A50') then 'OCA' 
        when t0.FatherNum = 'A90' then 'FA'
        when t0.FatherNum = 'A10' then 'CASH'
        else 'Other' end as "Classification"

        ,@to_date as "Date"
    
        ,t0."AcctName" "Account Name"

        ,case when t1.RefDate <= @to_date then  SUM(t1."Debit") - sum(t1."Credit") end as Balance


        FROM OACT t0 -- G/L Accounts

        inner join JDT1 t1 ON T0."AcctCode" = T1."Account"

        where t0."GroupMask" = 1
        

        group by
        t0."AcctName"
        ,t0.FatherNum  
        ,t1.RefDate
        ,t1."Account"


    ) as t

    pivot(
        sum(Balance)
        for "Date" in ([2021-01-31],[2021-02-28]))
    as pivot_table

I have revised my answer again.我再次修改了我的答案。 This will calculate the cumulative balance for each date less than current month as you require.这将根据您的需要计算小于当前月份的每个日期的累积余额。 Please check and let me know.请检查并让我知道。

      DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX),
    @colsForSelect AS NVARCHAR(MAX);
    
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(format(RefDate,'yyyy-MM')) 
            FROM JDT1 where refdate<DATEADD(month, DATEDIFF(month, 0, getdate()), 0)
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
               
SET @colsForSelect = STUFF((SELECT distinct ', Coalesce(' + (
STUFF((SELECT distinct '+ Coalesce(' + quotename(format(RefDate,'yyyy-MM'))+' ,0)'
            FROM JDT1 k where k.refdate<=j.refdate
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
) +',0) '+QUOTENAME(format(DATEADD(month, DATEDIFF(month, 0, RefDate), 0) ,'yyyy-MM-dd')) 
            FROM JDT1 j where refdate<DATEADD(month, DATEDIFF(month, 0, getdate()), 0)
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
            
set @query = 'Select Classification,"Account Name", ' + @colsForSelect + ' from 
            (
                 select
        ''Assets'' as "Group Mask"
        ,case when t0.FatherNum = ''A20'' then ''AR'' 
        when t0.FatherNum in (''A40'',''A50'') then ''OCA''
        when t0.FatherNum = ''A90'' then ''FA''
        when t0.FatherNum = ''A10'' then ''CASH''
        else ''Other'' end as "Classification"
        ,format(t1.RefDate ,''yyyy-MM'')as "Date"
    
        ,t0."AcctName" "Account Name"
        
        ,(SUM(t1."SYSDeb") + sum(t1."SYSCred"))  as Balance
 
        FROM OACT t0 -- G/L Accounts
        inner join JDT1 t1 ON T0."AcctCode" = T1."Account"
        where t0."GroupMask" = 1
        and t1.refdate <DATEADD(month, DATEDIFF(month, 0, getdate()), 0)
        
        group by
        t0."AcctName"
        ,t0.FatherNum  
        ,format(t1.RefDate,''yyyy-MM'')
        ,t1."Account"
           ) x
            pivot 
            (
                 SUM(balance)
                for date in (' + @cols + ')
            ) p 
            group by Classification,"Account Name",' + @cols 
 
execute(@query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM