简体   繁体   English

在SQL数据透视表的“总计”列中获取空值

[英]Getting null value in the Grand Total column in the Pivot Table in sql

I have the query which returning me the number of categories in one cloumn and other column are dynamic column they are giving me the the months between start date & end date and this column are returning me the amount of the categories sold on that month. 我有一个查询,该查询返回给我一个cloumn中的类别数,而其他列为动态列,它们给了我开始日期和结束日期之间的月份,并且此列向我返回了当月所售类别的数量。

I want to add the Grand Total at the end of the row in the query 我想在查询的行末添加总计

Here's My Query 这是我的查询

在此处输入图片说明

    DECLARE @cols AS NVARCHAR(MAX),
            @query  AS NVARCHAR(MAX),
            @subtotal AS FLOAT,
            @startdate as datetime,
            @enddate as datetime
            DECLARE @ColumnsRollup AS VARCHAR (MAX)

            set @startdate = '1-Mar-2014'
            set @enddate =   '1-Aug-2014'

;with cte (StartDate, EndDate) as
(
    select min(@startdate) StartDate, max(@enddate) EndDate    
    union all
    select dateadd(mm, 1, StartDate), EndDate
    from cte
    where StartDate < EndDate
) 
select  StartDate
into #tempDates
from cte 


select @cols =  STUFF((SELECT distinct ',' + QUOTENAME(convert(CHAR(10), StartDate, 120)) 
                    from #tempDates
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') +  ',[Total]' 






SET  @query =           'select  ledger_name,
                        ' + @cols + '                        
                        from
                        (
                        SELECT 


                       CASE WHEN (GROUPING(ledger_name) = 1) THEN ''Grand Total''
                       ELSE ledger_name END AS ledger_name,


                       ISNULL(SUM(amount),0) Amount, 

                       CASE WHEN (GROUPING(StartDate) = 1) THEN ''Total''
                       ELSE convert(CHAR(10), StartDate, 120) END StartDate

                       FROM #tempDates d
                       left join Rs_Ledger_Master AS LM on  d.StartDate between '''+convert(varchar(10), @startdate, 120)+''' and '''+convert(varchar(10), @enddate, 120)+'''                      
                       LEFT OUTER JOIN RS_Payment_Master AS PM ON PM.ledger_code = LM.ledger_code and  month(paid_date) = month(StartDate) and  year(paid_date) = year(StartDate) 
                       group by 
                       ledger_name,StartDate WITH ROLLUP              
                       ) d
                        pivot
                        (
                          SUM(Amount)
                          for StartDate in (' + @cols + ')
                        ) p                                                                 
                        ORDER BY CASE WHEN ledger_name = ''Grand Total'' THEN 1 END'
        execute sp_executesql @query;
        drop table #tempDates

I think what you can do is, try to use the ROLLUP option in your inner query, which actually returns addtional row for the SUM(Amount), which you can call as Total and add this column to your column list. 我认为您可以做的是,尝试在内部查询中使用ROLLUP选项,该查询实际上为SUM(Amount)返回附加行,您可以将其称为Total并将此列添加到列列表中。

Here is the change what I think you need to do 这是我认为您需要做的更改

Add total column at the end of your column list 在栏列表末尾添加总栏

SET @cols= @cols + ',[Total]'

Add the rollup option to your inner query. 将汇总选项添加到内部查询中。 Note that, the case statement is required to change the text as total for the row. 请注意,需要case语句才能将文本更改为该行的总计。

DECLARE @cols AS NVARCHAR(MAX),
            @query  AS NVARCHAR(MAX),
            @subtotal AS FLOAT,
            @startdate as datetime,
            @enddate as datetime
            DECLARE @ColumnsRollup AS VARCHAR (MAX)

            set @startdate = '1-Mar-2014'
            set @enddate =   '1-Aug-2014'

;with cte (StartDate, EndDate) as
(
    select min(@startdate) StartDate, max(@enddate) EndDate    
    union all
    select dateadd(mm, 1, StartDate), EndDate
    from cte
    where StartDate < EndDate
) 
select  StartDate
into #tempDates
from cte 


select @cols =  STUFF((SELECT distinct ',' + QUOTENAME(convert(CHAR(10), StartDate, 120)) 
                    from #tempDates
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') +  ',[Total]' 


SET  @query =           'select  ledger_name,
                        ' + @cols + '                        
                        from
                        (
                        SELECT 


                       ledger_name,


                       ISNULL(SUM(amount),0) Amount, 

                       CASE WHEN (GROUPING(StartDate) = 1) THEN ''Total''
                       ELSE convert(CHAR(10), StartDate, 120) END StartDate

                       FROM #tempDates d
                       left join Rs_Ledger_Master AS LM on  d.StartDate between '''+convert(varchar(10), @startdate, 120)+''' and '''+convert(varchar(10), @enddate, 120)+'''                      
                       LEFT OUTER JOIN RS_Payment_Master AS PM ON PM.ledger_code = LM.ledger_code and  month(paid_date) = month(StartDate) and  year(paid_date) = year(StartDate) 
                       group by 
                       ledger_name,StartDate WITH ROLLUP              
                       ) d
                        pivot
                        (
                          SUM(Amount)
                          for StartDate in (' + @cols + ')
                        ) p      
                        WHERE ledger_name IS NOT NULL
                         UNION ALL
                        select  ledger_name,
                        ' + @cols + ' FROM
                       (SELECT ''Grand Total'' AS ledger_name,
                       ISNULL(SUM(amount),0) Amount, 
                       CASE WHEN (GROUPING(StartDate) = 1) THEN ''Total''
                       ELSE convert(CHAR(10), StartDate, 120) END StartDate

                       FROM #tempDates d
                       left join Rs_Ledger_Master AS LM on  d.StartDate between '''+convert(varchar(10), @startdate, 120)+''' and '''+convert(varchar(10), @enddate, 120)+'''                      
                       LEFT OUTER JOIN RS_Payment_Master AS PM ON PM.ledger_code = LM.ledger_code and  month(paid_date) = month(StartDate) and  year(paid_date) = year(StartDate) 
                       group by StartDate WITH ROLLUP              
                       ) d
                       pivot
                       (
                         SUM(Amount)
                         for StartDate in (' + @cols + ')
                       ) p


                        '
        print @query
        execute sp_executesql @query;
        drop table #tempDates

You need additional union all to get the last summary row 您需要附加所有并集以获取最后的摘要行

You can refer the following article for using ROLLUP http://technet.microsoft.com/en-us/library/ms189305(v=sql.90).aspx 您可以参考以下文章以使用ROLLUP http://technet.microsoft.com/zh-cn/library/ms189305(v=sql.90).aspx

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

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