Home » SQL & PL/SQL » SQL & PL/SQL » Analytical Query AVG for different days in same sql (Oracle 10g/11g )
Analytical Query AVG for different days in same sql [message #622673] |
Thu, 28 August 2014 06:52 |
|
raopatwariyahoocom
Messages: 39 Registered: October 2011 Location: GA
|
Member |
|
|
In the following sql
I am trying to get average for 15 days for one column and rest for 21 days.. ( I am trying to recollect oracle analytic window function I have written but I have wasted two days)
SELECT network_ind, brand,pc_offer_id, soc, soc_category, rate
,pc_offer_desc, rc_type,
avg(total_charges) -- this should be average of 15 days
,avg(insufficient_funds_cnt) -- this should be average of 21 days,
avg(other_err_cnt) -- this should be average of 21 days, ,
avg(success_cnt) -- this should be average of 21 days
FROM cost_drc
WHERE report_date BETWEEN sysdate-21 AND sysdate-1
GROUP BY network_ind, brand,pc_offer_id, soc, soc_category, rate, pc_offer_desc, rc_type;
Detail notes as follows.....
1)
avg(total_charges) should be of 15 days
avg(insufficient_funds_cnt) should be of 21 days ,
avg(other_err_cnt) should be of 21 days ,
avg(success_cnt) should be of 21 days .
2)
Another thing is,
if the data is missing for a day (or couple of days) then a row has to be created for that to get correct average
in the following example
I have data for four days...
and we are missing the data for days from 9th thru 25th,
so rows should be created for those days to get correct average for 21 days
DATA
07-AUG-2014
08-AUG-2014
26-AUG-2014
27-AUG-2014
Lalit : Added code tags
[Updated on: Thu, 28 August 2014 06:57] by Moderator Report message to a moderator
|
|
|
|
Re: Analytical Query AVG for different days in same sql [message #622677 is a reply to message #622673] |
Thu, 28 August 2014 07:20 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
SELECT network_ind,
brand,
pc_offer_id,
soc,
soc_category,
rate,
pc_offer_desc,
rc_type,
sum(
case
when report_date BETWEEN sysdate - 16 AND sysdate - 1 then total_charges
end
) / 15, -- this should be average of 15 days
sum(insufficient_funds_cnt) / 21, -- this should be average of 21 days,
sum(other_err_cnt) / 21, -- this should be average of 21 days
sum(success_cnt) / 21 -- this should be average of 21 days
FROM cost_drc
WHERE report_date BETWEEN sysdate - 21 AND sysdate - 1
GROUP BY network_ind,
brand,
pc_offer_id,
soc,
soc_category,
rate,
pc_offer_desc,
rc_type
/
SY.
|
|
|
|
|
Re: Analytical Query AVG for different days in same sql [message #622683 is a reply to message #622680] |
Thu, 28 August 2014 07:53 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
I guess it is either I didn't understand your requirements or you didn't understand my suggestion. You have non-sequential (date-wise) data. Some days can be missing. Assume you have data for August 1, 2, 3, 20, 21 and you calculate AVG(success_cnt) Then Oracle will add success_cnt for existing days and will divide the results by 5 (since we only have data for 5 days). You want to add missing days with success_cnt = 0, so AVG(success_cnt) would divide by 21. My solution is much simpler. AVG(success_cnt) is nothing more but SUM(success_cnt) / COUNT(success_cnt), so all we need is SUM(success_cnt) / 21.
SY.
|
|
|
|
Goto Forum:
Current Time: Wed Apr 24 14:49:51 CDT 2024
|