-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
通用表模式
ACCOUNTID - 唯一標識符
收入 - 詮釋
ForecastDate - 日期時間
這是不可能建立。我想避免多個設置每月收入的UNION語句。有沒有辦法在第一個月(今天的月份)和我們數據庫中上個月末之間動態創建月份?