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 Go to next message
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 #622675 is a reply to message #622673] Thu, 28 August 2014 07:00 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Please read and follow the forum guidelines, to enable us to help you: Forum Guidelines and read How to use code tags

Please provide a working test case. Understand, we don't have your tables and data.

Re: Analytical Query AVG for different days in same sql [message #622677 is a reply to message #622673] Thu, 28 August 2014 07:20 Go to previous messageGo to next message
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 #622678 is a reply to message #622675] Thu, 28 August 2014 07:34 Go to previous messageGo to next message
raopatwariyahoocom
Messages: 39
Registered: October 2011
Location: GA
Member
Hello Lalith Kumar,
Appreciate your quick response. I have attached the file. Mr
Solomon Yakobson has responded the answer but only thing I ma looking for is missing data.
  • Attachment: export.csv
    (Size: 1.38KB, Downloaded 952 times)
Re: Analytical Query AVG for different days in same sql [message #622680 is a reply to message #622677] Thu, 28 August 2014 07:39 Go to previous messageGo to next message
raopatwariyahoocom
Messages: 39
Registered: October 2011
Location: GA
Member
Mr Solomon Yakobson,
Thank you very much for the response. That would be really useful. I have attached the attached the csv file which shows the missing data. I am not sure how to create missing data from 6 thru 16th.
  • Attachment: export.csv
    (Size: 1.38KB, Downloaded 973 times)
Re: Analytical Query AVG for different days in same sql [message #622683 is a reply to message #622680] Thu, 28 August 2014 07:53 Go to previous messageGo to next message
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.
Re: Analytical Query AVG for different days in same sql [message #622689 is a reply to message #622683] Thu, 28 August 2014 09:40 Go to previous message
raopatwariyahoocom
Messages: 39
Registered: October 2011
Location: GA
Member
Thanks , I think what you say make sense.
Previous Topic: Query to display string in vertical form
Next Topic: update a table based on another table with no specific order
Goto Forum:
  


Current Time: Wed Apr 24 14:49:51 CDT 2024