SQL每日滚动销售持续28天
我在计算滚动销售时遇到问题,我尝试过使用powerpivot,但由于我的电脑崩溃了大量事务,因此我需要在SQL Server中执行源查询 我需要一个新的列,每天与过去28天的销售总额,我有一个项目商店每天的销售交易 希望有人能帮助我,谢谢 非常感谢,SQL每日滚动销售持续28天,sql,powerpivot,Sql,Powerpivot,我在计算滚动销售时遇到问题,我尝试过使用powerpivot,但由于我的电脑崩溃了大量事务,因此我需要在SQL Server中执行源查询 我需要一个新的列,每天与过去28天的销售总额,我有一个项目商店每天的销售交易 希望有人能帮助我,谢谢 非常感谢, JR假设您希望汇总按天和按项目Nbr分组的Pos数量,并且您希望在最后28天有28个不同的列,您的查询可能如下所示: select [Store...], [Item Nbr], sum(case when cast([Daily
JR假设您希望汇总按天和按
项目Nbr分组的Pos数量
,并且您希望在最后28天有28个不同的列,您的查询可能如下所示:
select [Store...], [Item Nbr],
sum(case when cast([Daily] as date) = cast(getdate() as date) then [POS Qty] end) SumDay0,
sum(case when cast([Daily] as date) = cast(getdate() - 1 as date) then [POS Qty] end) SumDay_1,
sum(case when cast([Daily] as date) = cast(getdate() - 2 as date) then [POS Qty] end) SumDay_2,
sum(case when cast([Daily] as date) = cast(getdate() - 3 as date) then [POS Qty] end) SumDay_3,
--...
sum(case when cast([Daily] as date) = cast(getdate() - 27 as date) then [POS Qty] end) SumDay_27
from [your table]
group by [Store...], [Item Nbr]
当
语句时,此查询对每个求和使用一个case,以便仅过滤您正在查看的某一天的数据。然后对语句求和时的情况的结果
当然,如果您希望结果是行而不是列,则需要以不同的方式编写查询。如果您有每天的数据,则使用窗口功能:
select t.*,
sum(qty) over (partition by store, item order by daily rows between 27 preceding and current row) as avg_28day
from t;
欢迎来到StackOverflow。我不确定你到底想根据你发布的内容解决什么问题。这可能值得回顾一下。看起来可能会给你指明正确的方向。我还想先算出一个每日总计,然后再从中算出滚动总计。您可以使用sum()over()
上的窗口框架,也可以只使用一个标量子查询。