SQL - 交叉表。字段的总和而不是列中的计数行

时间:2017-07-28 12:57:50

标签: sql ssms crosstab

目前正在尝试创建一个查询,该查询将为我提供全年代理商的销售额。我已经设法让总列工作,但是每日销售不会,从查看原始数据看起来,而不是总结特定代理的每日总销售量而不是计算所有行代理列出并输出该代替。 代码如下:

DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT  ',' + QUOTENAME(convert(varchar(20), [ENTRY_DATE], 106)) 
                  FROM [Sales_Data] with (NoLock) 
                where([ENTRY_DATE] between '01-Jan-2017' and '01-Jul-2017'


                Group By [ENTRY_DATE]
                order by [ENTRY_DATE]
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

set @query = 

' SELECT 
  [Advisor_Name]                                                                AS    [Advisor_Name]
 ,[Line_Managers_Name]                                                          AS    [Line_Managers_Name]
 ,[SALE_TYPE]
 ,[Total]
 ,'+ @cols +'

    From  (
            Select 
                    sum([SALES]) 
                                over(partition by [Advisor_Name], [Line_Managers_Name], [SALE_TYPE]) 
                                      [Total]
                                ,    [ENTRY_DATE]
                                ,    [Advisor_Name]
                                ,     [Line_Managers_Name]
                                ,    [SALE_TYPE]


            From [Sales_Data] a with (NoLock)        



        )     x

    pivot 

    (
                                      Count([ENTRY_DATE])
                                      for [ENTRY_DATE] in (' + @cols + ')
    ) p 


Order by    [Advisor_Name],     [Line_Managers_Name],     [CHANNEL]          

'

execute(@query)

如果我无法更改我原先计划的查询,而是更改源数据,以便逐行销售信息,而不是汇总为总和,不幸的是,某些行可能会有一个客户的-1销售到表示退款,我不知道如何适应这些,我尝试选择不同,选择(选择不同.....)有计数(*)= 1,但似乎没有什么可以工作。

2 个答案:

答案 0 :(得分:0)

你可以尝试一下,我必须尝试模拟你的数据,但看起来我的效果很好。

 SELECT 
 *
    From  (
Select distinct
sum([Total]) 
over(partition by [Advisor_Name], [Line_Managers_Name], [SALE_TYPE],    [ENTRY_DATE]) as Total
,    [ENTRY_DATE]
,    [Advisor_Name]
,     [Line_Managers_Name]
,    [SALE_TYPE],
sum([Total]) 
over(partition by [Advisor_Name], [Line_Managers_Name], [SALE_TYPE]) as TotalSales

            From [Sales_Data] a with (NoLock)        

        )     x
    pivot 
    (
          Sum(Total)
          for [ENTRY_DATE] in ([01 Jan 2017],[02 Jan 2017],[03 Jan 2017])
    ) p 


Order by    [Advisor_Name],     [Line_Managers_Name]

答案 1 :(得分:0)

我还修改了动态生成的SQL以生成新的输出。

 DECLARE @cols AS NVARCHAR(MAX)
DECLARE @colsForPivot AS NVARCHAR(MAX)
DECLARE @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT  ',ISNULL(' + QUOTENAME(convert(varchar(20), [ENTRY_DATE], 106)) + ',0)'
                  FROM [Sales_Data] with (NoLock) 
                where([ENTRY_DATE] between '01-Jan-2017' and '01-Jul-2017')
                Group By [ENTRY_DATE]
                order by [ENTRY_DATE]
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

select @colsForPivot = STUFF((SELECT  ',' + QUOTENAME(convert(varchar(20), [ENTRY_DATE], 106))
              FROM [Sales_Data] with (NoLock) 
            where([ENTRY_DATE] between '01-Jan-2017' and '01-Jul-2017')
            Group By [ENTRY_DATE]
            order by [ENTRY_DATE]
    FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)') 
,1,1,'')

set @query = 

'SELECT 

[Advisor_Name], [Line_Managers_Name], [SALE_TYPE], ISNULL(TotalSales,0), '
+ @cols + ' 

    From  (
            Select distinct
                    sum([SALES]) 
                                over(partition by [Advisor_Name], [Line_Managers_Name], [SALE_TYPE],    [ENTRY_DATE]) 
                                      [Total]
                                ,    [ENTRY_DATE]
                                ,    [Advisor_Name]
                                ,     [Line_Managers_Name]
                                ,    [SALE_TYPE],
sum([SALES]) 
over(partition by [Advisor_Name], [Line_Managers_Name], [SALE_TYPE]) as TotalSales


            From [Sales_Data] a with (NoLock)        



        )     x

    pivot 

    (
                                      sum([Total]) 
                                      for [ENTRY_DATE] in (' + @colsForPivot + ')
    ) p 


Order by    [Advisor_Name],     [Line_Managers_Name]      

'

print(@query)