使用 sql 创建动态月份列

Create dynamic months columns with sql

我已经对我们的帐户和每个月的收入总额进行了分组查询。我基本上想为每个月创建一个带有存储桶的临时 table,如果该记录存​​在,则将收入添加到该月,因为此查询每个月只有 returns 记录。

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

Generic Table Schema
AccountId - uniqueidentifier
Revenue - int
ForecastDate - DateTime

这个可以建吗。我想避免使用多个 set UNION 语句来拉动每个月的收入。有没有办法在数据库中的第一个月(今天的月份)和最后一个月的月底之间动态创建月份?

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