我有问题将下面的查询分组为每月汇总。 表与一个表交叉连接以获取速率,内部与另一个连接,其中只包含日期以显示表中数据不存在的日期的空值(客户端请求) 它适用于下面的每日分组。请如何每月分组。
Select * from(select [Letter_Date] [Date],Council
SUM([Total_Corr])*[Rate][Total]
FROM Correspondence
cross join
Input_Variable_Price
where [Revenue_Name] = 'Correspondence'
group by [Letter_Date],Council)AS ED
RIGHT JOIN
(Select '21'[No],b_date,[Revenue_Name][Report],[Unit],[Rate]
From Blank_dates
cross join
Input_Variable_Price
where [Revenue_Name] = 'Correspondence') AS BD
ON ED.Date = BD.[b_date]
干杯
答案 0 :(得分:1)
我会使用以下内容:添加SELECT
所需的任何其他聚合,以及GROUP BY
中您需要的任何项目。
Select DATEADD(month, DATEDIFF(month, 0, [Date]), 0) AS StartOfMonth, SUM(Total)
from
(
select [Letter_Date] [Date],Council,
SUM([Total_Corr])*[Rate] [Total]
FROM
Correspondence
cross join
Input_Variable_Price
where [Revenue_Name] = 'Correspondence'
group by [Letter_Date],Council
)AS ED
RIGHT JOIN
(
Select
'21'[No],
b_date,
[Revenue_Name][Report],
[Unit],
[Rate]
From
Blank_dates
cross join
Input_Variable_Price
where [Revenue_Name] = 'Correspondence'
) AS BD ON
ED.Date = BD.[b_date]
GROUP BY DATEADD(month, DATEDIFF(month, 0, [Date]), 0)