-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语句。有没有办法在第一个月(今天的月份)和我们数据库中上个月末之间动态创建月份?