2015-08-24 72 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