使用sql创建动态月份列

时间:2015-08-24 18:09:48

标签: sql-server tsql union pivot-table dynamic-sql

我已经提出了一个查询,将我们的帐户和每个月的收入总和分组。我基本上想要为每个月创建一个带有存储桶的临时表,如果存在该记录,则将收入添加到该月中,因为此查询仅返回每个月的记录。

SELECT 
    p.New_AccountId AS AccountId,
    Account.Name AS AccountName,
    SUM(pf.New_Revenue) AS ForecastRevenue,
    pf.New_ForecastDate AS ForecastDate
FROM 
    ProfitRecoveryPartnersLLC_MSCRM.dbo.New_projectforecast AS pf 
INNER JOIN
    ProfitRecoveryPartnersLLC_MSCRM.dbo.New_project AS p ON p.New_projectId = pf.New_ProjectId 
INNER JOIN
    ProfitRecoveryPartnersLLC_MSCRM.dbo.Account ON pf.New_AccountId = Account.AccountId 
INNER JOIN
    (SELECT 
         Account.AccountId, SUM(pf.New_Revenue) AS ForecastMonthsRevenue
     FROM 
         ProfitRecoveryPartnersLLC_MSCRM.dbo.New_project AS p 
     INNER JOIN
         ProfitRecoveryPartnersLLC_MSCRM.dbo.New_projectforecast AS pf ON p.New_projectId = pf.New_ProjectId 
     INNER JOIN
         ProfitRecoveryPartnersLLC_MSCRM.dbo.Account ON pf.New_AccountId = Account.AccountId 
     WHERE 
         pf.statuscode = 1 GROUP BY AccountId) AS ForecastMonths ON ForecastMonths.AccountId = pf.New_AccountId
WHERE 
    pf.statuscode = 1
GROUP BY 
    p.New_AccountId, Account.Name, pf.New_ForecastDate
ORDER BY 
    Account.Name

enter image description here

  

通用表格模式
  AccountId - uniqueidentifier
  收入 - int
  ForecastDate - DateTime

这是否可以构建。我想避免多个集合的UNION语句,每个月都会带来收入。有没有办法在第一个月(今天的月份)和我们在数据库中的最后一个月末之间动态创建月份?

Need to mimick something like this using sql

1 个答案:

答案 0 :(得分:1)

SELECT 
    p.New_AccountId AS AccountId,
    Account.Name AS AccountName,
    SUM(pf.New_Revenue) AS ForecastRevenue,
    cast(Datepart(mm,pf.New_ForecastDate) as varchar) + '-' + cast(Datepart(YYYY,pf.New_ForecastDate) as varchar) as MonthYear
    into #temp
FROM 
    ProfitRecoveryPartnersLLC_MSCRM.dbo.New_projectforecast AS pf 
INNER JOIN
    ProfitRecoveryPartnersLLC_MSCRM.dbo.New_project AS p ON p.New_projectId = pf.New_ProjectId 
INNER JOIN
    ProfitRecoveryPartnersLLC_MSCRM.dbo.Account ON pf.New_AccountId = Account.AccountId 
INNER JOIN
    (SELECT 
         Account.AccountId, SUM(pf.New_Revenue) AS ForecastMonthsRevenue
     FROM 
         ProfitRecoveryPartnersLLC_MSCRM.dbo.New_project AS p 
     INNER JOIN
         ProfitRecoveryPartnersLLC_MSCRM.dbo.New_projectforecast AS pf ON p.New_projectId = pf.New_ProjectId 
     INNER JOIN
         ProfitRecoveryPartnersLLC_MSCRM.dbo.Account ON pf.New_AccountId = Account.AccountId 
     WHERE 
         pf.statuscode = 1 GROUP BY AccountId) AS ForecastMonths ON ForecastMonths.AccountId = pf.New_AccountId
WHERE 
    pf.statuscode = 1
GROUP BY 
    p.New_AccountId, Account.Name, pf.New_ForecastDate
ORDER BY 
    Account.Name

------------------------------------------------------------------
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME(MonthYear)
FROM (SELECT DISTINCT MonthYear FROM #temp) AS Courses

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT *
    FROM
  (SELECT AccountId, AccountName, MonthYear, ForecastRevenue
    FROM #temp) AS Sales
  PIVOT(SUM(BookSales)  
          FOR MonthYear IN (' + @ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery