对某些行执行算术运算并显示结果

时间:2013-05-02 12:35:13

标签: oracle oracle11g oracle10g jasper-reports

以下是我的表格内容:

select * from summary_weekly_sales;

DISTRIBUTOR    DATE_OF_ACTIVATION  NUMBER_OF_SALES
-------------- ------------------  ---------------
charan          25-APR-13              23
charan          26-APR-13               2
charan          28-APR-13               5
charan          29-APR-13              50
anil            25-APR-13              13
anil            26-APR-13               4
anil            28-APR-13               5
anil            29-APR-13              30

在ireport中,DATE_OF_ACTIVATION是输入参数(但这里我将date_of_activation视为29-APR-13),我希望输出显示如下:

DISTRIBUTOR    avg_sales_week   NUMBER_OF_SALES
-------------- ---------------  ---------------
charan          10              50

anil            7.33            30

其中,

avg_sales_week 是每个经销商的平均周销售额(即29-APR-13的7天后)

即。 charan 经销商平均值=(5 + 2 + 23)/ 3

Number_Of_Sales 是在29-APR-13上完成的销售

我尝试使用oracle的wm_concat函数,但它没有按预期工作。

有没有办法获得上述预期结果。

此致 查兰

2 个答案:

答案 0 :(得分:2)

这样就可以了:

select distributor
,      sum(case when date_of_activation < date '2013-04-29'
           then number_of_sales end)  
       / count(distinct case when date_of_activation < date '2013-04-29' 
           then date_of_activation end) as avg_sales_week   
,      sum(case when date_of_activation=date '2013-04-29' 
           then number_of_sales end) number_of_sales
from   summary_weekly_sales
where  date_of_activation between date '2013-04-29' - 7 and date '2013-04-29'
group by distributor;

DISTRIBUTO AVG_SALES_WEEK NUMBER_OF_SALES
---------- -------------- ---------------
anil           7.33333333              30
charan                 10              50

只需用参数名称替换date '2013-04-29',例如p_date在程序中使用。

答案 1 :(得分:0)

select 
distributor, 
sum(number_of_sales) / 
    (select count(*) from summary_weekly_sales sws2 where sws2.distributor = distributor and sws2.date_of_activation > '29-APR-13' - 7)
as avg_sales_week, 
number_of_sales
FROM summary_weekly_sales
WHERE DATE_OF_ACTIVATION > '29-APR-13' - 7
GROUP BY distributor

也许这应该有所帮助。