每天获取产品销售额的平均值并计算销售额为正的天数
Posted
技术标签:
【中文标题】每天获取产品销售额的平均值并计算销售额为正的天数【英文标题】:Getting average of product sales each day and calculate number of days that have positive sales 【发布时间】:2014-10-11 07:58:05 【问题描述】:我有这张表 TARGETSALE 有以下列
SELECT DATE, WEEK, BRANCH, PROD, TARGETREACH
FROM TARGETSALE
WHERE BRANCH = 1
AND WEEK BETWEEN 52 AND 53;
DATE WEEK BRANCH PROD TARGETREACH
-------------------------------------------------------------------
01/09/2014 52 1 1 50
02/09/2014 52 1 1 -10
03/09/2014 52 1 1 50
04/09/2014 52 1 1 50
05/09/2014 52 1 1 40
06/09/2014 52 1 1 -10
07/09/2014 53 1 1 -5
08/09/2014 53 1 1 0
09/09/2014 53 1 1 10
10/09/2014 53 1 1 20
11/09/2014 53 1 1 30
12/09/2014 53 1 1 40
13/09/2014 53 1 1 0
01/09/2014 52 1 2 20
02/09/2014 52 1 2 0
03/09/2014 52 1 2 0
04/09/2014 52 1 2 10
05/09/2014 52 1 2 20
06/09/2014 52 1 2 10
07/09/2014 53 1 2 -10
08/09/2014 53 1 2 10
09/09/2014 53 1 2 -10
10/09/2014 53 1 2 20
11/09/2014 53 1 2 20
12/09/2014 53 1 2 40
13/09/2014 53 1 2 0
01/09/2014 52 1 3 30
02/09/2014 52 1 3 30
03/09/2014 52 1 3 5
04/09/2014 52 1 3 0
05/09/2014 52 1 3 10
06/09/2014 52 1 3 -10
07/09/2014 53 1 3 -10
08/09/2014 53 1 3 -10
09/09/2014 53 1 3 20
10/09/2014 53 1 3 10
11/09/2014 53 1 3 40
12/09/2014 53 1 3 10
13/09/2014 53 1 3 10
“targetsales”显示销售额超出目标多少,其中负数表示销售额低于目标多少。我该如何执行以下操作: 1. 我需要获取每天所有产品的平均值。像这样的:
DATE BRANCH AVERAGE_SALES_OF_ALL_PRODUCT
01/09/2014 1 33.33
02/09/2014 1 -1.67
...and so on
然后我需要另一个查询来显示这两周内有多少天平均销售额为正。像这样的:
BRANCH 2WEEKS_SINCE DAYS_WITH_POSITIVE_AVERAGE_SALES
1 53 9
以上只是一个示例,并非真实结果。 对不起,希望这不会太混乱。非常感谢。
【问题讨论】:
虽然使用 SQL 服务器而不是 Oracle。但它应该工作。第一个 -select [date], branch, sum(targetreach)/COUNT(prod) as [avg] from targetsale group by [date], branch
@BoratSagdiyev,谢谢!第二个怎么样? :)
【参考方案1】:
在 Oracle 中,date
类型可能仍具有时间组件。如果您不知道是否存在,请使用trunc()
将其删除:
select trunc(date), branch, avg(targetreach)
from targetsale
group by truncdate, branch
order by 1, 2;
对于第二个查询,您想使用case
:
select branch, count(distinct case when targetreach > 0 then date end) as DaysWithPositiveSales
from targetsales
group by branch;
如果您知道每个分支的每个日期有一行 - 并且日期的时间部分为空 - 那么distinct
就没有必要了。
【讨论】:
【参考方案2】:1)
SELECT TRUNC(DATE, 'DD'), BRANCH, SUM(TARGETREACH)
FROM TARGETSALE WHERE BRANCH = 1 AND WEEK BETWEEN 52 AND 53
GROUP BY TRUNC(DATE, 'DD'), BRANCH;
2)
SELECT BRANCH, SUM(DECODE(ABS(TARGETREACH), 1, 1, 0)
FROM TARGETSALE WHERE BRANCH = 1 AND WEEK BETWEEN 52 AND 53
GROUP BY BRANCH;
【讨论】:
以上是关于每天获取产品销售额的平均值并计算销售额为正的天数的主要内容,如果未能解决你的问题,请参考以下文章