正在处理一项任务,以下是问题,她已指示我们使用 CTE
编写用于探索数据库表的 SQL 查询代码并编写一个查询,从“AdventureWorksDW2016CTP3”数据库中的“FactFinance”检索财务金额并返回这些金额,按月组织,并显示 3 个月的滚动平均值
SELECT DateKey,
month(date) as [Month],
year(date) as [Year],
SUM ( ALL Amount) OVER (PARTITION BY Date ORDER BY Date ASC) AS Amount
FROM FactFinance
SELECT
YEAR(Date) AS Year,
MONTH(Date) AS Month,
SUM(Amount) AS Amount
FROM FactFinance
GROUP BY YEAR(Date), MONTH(Date)
ORDER BY Year, Month;
WITH CTE AS (
SELECT
DateKey AS Month,
AVG(Amount) AS AvgAmt
from FactFinance
group by DateKey
)
SELECT
Month,
AvgAmt
FROM CTE
GO
最后一次查询的 OUTPUT 需要 3 个月的滚动平均值