计算同一行中的每月和最后十二个日期
calculate monthly and twelve last date in the same row
我有 table 销售额
Activity | Sector | dateSale | Amount
CREATE TABLE sales
(Activity nvarchar(100) ,
Sector nvarchar(100) ,
dateSale date ,
Amount as float )
我需要在同一行中计算每月和最后一个月的十二个日期:
预期结果例如:
Activity | Sector | AmountMonth | AmountLTD
我试着计算一个月的金额
declare @date as date,
set @date ='17-11-2020'----- I should to calculate amount in the previous month of the date parameter: all october for the same year
SELECT SUM(Amount)
,Activity
,Sector
FROM sales
WHERE [dateSale] between DATEADD(MONTH, DATEDIFF(MONTH, 0, @date)-1, 0)
and DATEADD(MONTH, DATEDIFF(MONTH, -1, @date)-1, -1)
group by Activity
,Sector
我试图计算 LTD 中的金额但是它是错误的,我需要更正它:
declare @date as date,
set @date ='17-11-2020'----- I should to calculate amount in the ltd of the date parameter Ineed to calculate from november 2019 to october 2020
SELECT SUM(Amount)
,Activity
,Sector
FROM sales
WHERE [dateSale] between DateAdd(DAY,-Day(@Date)+1,@Date)
and DateAdd(MONTH,-13,@DateR2)
group by Activity
,Sector
请问如何在预期的同一行中进行计算?有没有其他简单的计算方法?
如果您想要每个月的金额以及后面 12 个月的金额,那么您需要分别计算总和。从您的问题中不清楚您想要输出的确切方式,但希望以下内容会有所帮助。它计算内部查询中每个月的总和,并在外部查询中附加尾随的 12。
SELECT s.Activity, s.Sector, s.StartOfMonth, s.AmountMonth, SUM(salesLtd.Amount) [AmountLTD]
FROM (
SELECT Activity, Sector, SUM(Amount) [AmountMonth], StartOfMonth
FROM sales
CROSS APPLY (SELECT DATEADD(month, DATEDIFF(month, 0, dateSale), 0) [StartOfMonth]) [forStart]
GROUP BY Activity, Sector, StartOfMonth
) s
INNER JOIN sales salesLtd ON salesLtd.dateSale <= EOMONTH(s.StartOfMonth)
AND salesLtd.dateSale >= DATEADD(year, -1, s.StartOfMonth)
GROUP BY s.Activity, s.Sector, s.StartOfMonth, s.AmountMonth
我本可以使用 CTE 构建内部查询,然后在外部查询的连接中使用相同的 CTE(大概是为了利用已经聚合的数据),但我怀疑它实际上会更慢。
我有 table 销售额
Activity | Sector | dateSale | Amount
CREATE TABLE sales
(Activity nvarchar(100) ,
Sector nvarchar(100) ,
dateSale date ,
Amount as float )
我需要在同一行中计算每月和最后一个月的十二个日期: 预期结果例如:
Activity | Sector | AmountMonth | AmountLTD
我试着计算一个月的金额
declare @date as date,
set @date ='17-11-2020'----- I should to calculate amount in the previous month of the date parameter: all october for the same year
SELECT SUM(Amount)
,Activity
,Sector
FROM sales
WHERE [dateSale] between DATEADD(MONTH, DATEDIFF(MONTH, 0, @date)-1, 0)
and DATEADD(MONTH, DATEDIFF(MONTH, -1, @date)-1, -1)
group by Activity
,Sector
我试图计算 LTD 中的金额但是它是错误的,我需要更正它:
declare @date as date,
set @date ='17-11-2020'----- I should to calculate amount in the ltd of the date parameter Ineed to calculate from november 2019 to october 2020
SELECT SUM(Amount)
,Activity
,Sector
FROM sales
WHERE [dateSale] between DateAdd(DAY,-Day(@Date)+1,@Date)
and DateAdd(MONTH,-13,@DateR2)
group by Activity
,Sector
请问如何在预期的同一行中进行计算?有没有其他简单的计算方法?
如果您想要每个月的金额以及后面 12 个月的金额,那么您需要分别计算总和。从您的问题中不清楚您想要输出的确切方式,但希望以下内容会有所帮助。它计算内部查询中每个月的总和,并在外部查询中附加尾随的 12。
SELECT s.Activity, s.Sector, s.StartOfMonth, s.AmountMonth, SUM(salesLtd.Amount) [AmountLTD]
FROM (
SELECT Activity, Sector, SUM(Amount) [AmountMonth], StartOfMonth
FROM sales
CROSS APPLY (SELECT DATEADD(month, DATEDIFF(month, 0, dateSale), 0) [StartOfMonth]) [forStart]
GROUP BY Activity, Sector, StartOfMonth
) s
INNER JOIN sales salesLtd ON salesLtd.dateSale <= EOMONTH(s.StartOfMonth)
AND salesLtd.dateSale >= DATEADD(year, -1, s.StartOfMonth)
GROUP BY s.Activity, s.Sector, s.StartOfMonth, s.AmountMonth
我本可以使用 CTE 构建内部查询,然后在外部查询的连接中使用相同的 CTE(大概是为了利用已经聚合的数据),但我怀疑它实际上会更慢。