创建动态查询以报告每个公司组每月的产品数量,以及每个产品和每个公司的总数

时间:2018-05-11 20:07:08

标签: sql-server dynamic pivot-table subtotal totals

应创建动态数据透视表以报告公司组每月销售的产品。

  • 有几种产品,每个公司集团可能有不同的组合。
  • 产品状态为“有效”和“已取消”。
  • 使用结算日期(dbilldate)计算有效产品,并使用dtContractCancelledDate计算已取消的产品。
  • 在某些情况下,公司集团在本月内不会销售或取消产品,因此所有列都应反映出来(请查看Prod2已取消)。 应存在计算列以计算每个产品的净值(Prod Active - Prod Cancelled)。

我们需要帮助

•使用相应的日期(dbilled和dtcancelledbilled)创建不同的计算以计算产品的活动和取消

•当没有售出/取消的产品时,创建一个0的列

•为每个产品创建净列

•在每列的末尾创建小计

这是期望的结果:

  Pd1Actv Pd1Cancd  Pd1Net  Prd2Actv Prd2Cancd  Prd2Net   Total
Comp1    6        5        1       15        0        15       16
Comp2   20        6       14       39        0        39       53
Comp3   63       14       49       82        0        82      131
Total   89       25       64      136        0       136      200

这是提取数据的主表

SELECT [iCompId],[sContractStatusCode],[sContractStatusDesc],[dBillDate] ,dtContractCancelledBilled  
FROM [Contract_Header]

这就是我现在所拥有的:

DECLARE 
@cols AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX),
@Compgroupnumber as varchar(20),
@startdate as date,
@enddate as date

set @startdate=cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) as
date)
set @enddate=DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
set @sCompGroupNumber ='DG10000174'

select @cols = 
STUFF((SELECT distinct  ',' + QUOTENAME(h.sProductCode+'
 '+sContractStatusDesc) 
from [Contract_Header] h
inner join [Comp_Header] d on d.iid=h.icompId
inner join [Comp_CompGroupLink] dgl on d.iId=dgl.CompId
inner join [Comp_DealerGroups] dg on dgl.iCompGroupId=dg.iId
where  (h.sContractStatusCode='A' or h.sContractStatusCode='C') and
(h.dBillDate between @startdate and @enddate)  and
sCompGroupNumber='DG10000174' and 
h.sProductCode in
(                     
  select distinct t.sProductCode
  from [Contract_Header] t
  inner join [Comp_Header] d on d.iid=t.iDealerId
  inner join [Comp_CompGroupLink] dgl on d.iId=dgl.iCompId
  inner join [Comp_CompGroups] dg on dgl.iDealerGroupId=dg.iId
  where  (t.sContractStatusCode='A' or t.sContractStatusCode='C') and
 (t.dBillDate between @startdate and @enddate)  and
  sCompGroupNumber='DG10000174' 
  )FOR XML PATH('')) ,1,1,'')

set @query= 
'SELECT sCompName,' + @cols + ' , +Total
 from 
(
select d.sCompName,
 col = c.sProductCode+'' ''+c.sContractStatusDesc,c.sContractStatusCode,
from [Contract_Header] c
 inner join [Comp_Header] d on d.iid=c.iDealerId
 inner join [Comp_CompGroupLink] dgl on d.iId=dgl.iCompId
 inner join [Comp_CompGroups] dg on dgl.iCompGroupId=dg.iId
 where (dBillDate between '''+CAST(@startdate AS varchar)+''' and 
         '''+CAST(@enddate AS VARCHAR)+''') and 
        sCompGroupNumber='''+'DG10000174'+''' and  
        (c.sContractStatusCode='''+'A'+''' or 
         c.sContractStatusCode='''+'C'+''' ) and 
            sproductcode in

            (select distinct sProductCode 
             from [Contract_Header] t 
              inner join [Comp_Header] d on d.iid=t.iCompId
              inner join [Comp_CompGroupLink] dgl on d.iId=dgl.iCompId
              inner join [Comp_CompGroups] dg on dgl.iCompGroupId=dg.iId
              where (dBillDate between '''+CAST(@startdate AS varchar)+''' 
              and '''+CAST(@enddate AS VARCHAR)+''') and             
              (t.sContractStatusCode='''+'A'+''' or 
               t.sContractStatusCode='''+'C'+''' ) and 
               sCompGroupNumber='''+'DG10000174'+''' 
             ) 
      )  as DataSource
        pivot 
        (
            Count(sContractStatusCode)
            for col in (' + @cols + ')
        ) p order by sCompName' 

execute sp_executesql @query

这是上面查询的结果

    Pd1Actv Pd1Cancd Pd2Actv Pd3Actv    
Comp 1    59      0         59     118      
Comp 2    26      1         25      65      
当没有为Pd2和Pd3取消产品,净列(pdAct-PdCanc),子总数和总数时,

缺少列。

提前感谢所有帮助...

玛丽

0 个答案:

没有答案