2015-08-24 74 views
-1

我已经提出了一个查询,它将我们的帐户和每个月的收入总和分组。我基本上想创建一个临时表,每个月都有一个桶,并且如果该记录存​​在,则将收入添加到该月,因为此查询仅返回每个月的记录。使用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 - 唯一标识符
收入 - 诠释
ForecastDate - 日期时间

这是不可能建立。我想避免多个设置每月收入的UNION语句。有没有办法在第一个月(今天的月份)和我们数据库中上个月末之间动态创建月份?

Need to mimick something like this using sql

回答

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