[英]calculate weekly and monthly sales from daily sales
If I have daily sales, how do I show weekly and monthly sales along with daily in a single record in oracle?如果我有每日销售额,我如何在 oracle 的单个记录中显示每周和每月的销售额以及每天的销售额? I can calculate weekly sum and monthly sum in separate tables, but I want to the results in a single data set.我可以在单独的表中计算每周总和和每月总和,但我想在单个数据集中计算结果。
Output should look like shown below.输出应如下所示。
Date Week Month Daily_Sale Weekly_Sale Monthly_Sale
1/1/20 1 1 $5 $5 $5
1/2/20 1 1 $5 $10 $10
1/3/20 1 1 $1 $11 $11
1/4/20 1 1 $2 $13 $13
1/5/20 1 1 $5 $18 $18
1/6/20 1 1 $1 $19 $19
1/7/20 1 1 $1 $20 $20
1/8/20 2 1 $5 $5 $25
1/8/20 2 1 $5 $10 $30
1/10/20 2 1 $1 $11 $31
1/11/20 2 1 $2 $13 $33
1/12/20 2 1 $5 $18 $38
1/13/20 2 1 $1 $19 $39
1/14/20 2 1 $1 $20 $40
Thank you!谢谢!
Edit: Highlighting the table编辑:突出显示表格
You seem to want running totals.您似乎想要运行总计。 Assuming that your tables contains sales information for more than a single year you need to partition on year as well.假设您的表包含超过一年的销售信息,您还需要按年份进行分区。
select Date, extract(week from Date) Wk, extract(year from Date) Yr,
Daily_Sale,
sum(Daily_Sale) over (
partition by extract(year from Date), extract(week from Date)
order by Date
) as Weekly_Sale
sum(Daily_Sale) over (
partition by extract(year from Date), extract(month from Date)
order by Date
) as Monthly_Sale
from T
order by Date;
I don't have oracle compiler so I replicated the scenario in SSMS.我没有 oracle 编译器,所以我在 SSMS 中复制了这个场景。 Here is the query:这是查询:
SELECT T1.D1, T1.AnnualSales, T1.MonthlySales, T1.WeeklySales, T1.DailySales
From
(SELECT [Date] As D1,
Sum([Sales]) Over (Partition by Year(Date)) as AnnualSales,
Sum([Sales]) Over (Partition by Month(Date)) as MonthlySales,
Sum([Sales]) Over (Partition by Datepart(wk,Date)) as WeeklySales,
Sum([Sales]) Over (Partition by Day(Date)) as DailySales
FROM [dbo].[DailySales_Test]) AS T1
Group by T1.D1, T1.AnnualSales, T1.MonthlySales, T1.WeeklySales, T1.DailySales
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.