Oracle SQL: How to select the last available value in a month? -
Oracle SQL: How to select the last available value in a month? -
i have series of daily data, though values not available days. , want select lastly value of every month, , match date end day of month. if no observations available month, add together month result , homecoming null.
for example, original table is:
as_of_date dailyavg 24-oct-13 12.00.00.000000000 71.61 29-oct-13 12.00.00.000000000 59.26 30-oct-13 12.00.00.000000000 57.29 31-oct-13 12.00.00.000000000 55.44 22-nov-13 12.00.00.000000000 0 27-nov-13 12.00.00.000000000 0 29-nov-13 12.00.00.000000000 0 15-jan-14 12.00.00.000000000 195.83 28-jan-14 12.00.00.000000000 537.83 29-jan-14 12.00.00.000000000 519.28 30-jan-14 12.00.00.000000000 501.97 31-jan-14 12.00.00.000000000 485.78 06-feb-14 12.00.00.000000000 119.79 07-feb-14 12.00.00.000000000 79.86 28-feb-14 12.00.00.000000000 588.28 31-mar-14 12.00.00.000000000 2315.56
and want code return:
dates monavg 31-oct-13 55.44 30-nov-13 0 31-dec-13 null 31-jan-14 485.78 28-feb-14 588.28 31-mar-14 2315.56
my current code following:
select b.dates, monavg (select distinct last_day(as_of_date) dates, last_value(dailyavg) on (partition trunc(as_of_date, 'mon')) monavg -- wrong in line from( select as_of_date, sum(avg_balance_usd) dailyavg balances grouping as_of_date)) right bring together (select distinct last_day(as_of_date) dates balances) b --this table of right dates on a.dates=b.dates order dates;
the code runs, returning wrong values.
my info pretty huge, not easy post how results like. , not able find out why got wrong values...
can figure out wrong in code?
thank you!
first create subset of lastly days per month (tip: utilize connect trick or create "dates" table) then create subset of lastly daily average per month (max(daily_avg) maintain (dense_rank ..) ) then left bring together 2 subsets
also seek indentation queries. improves readability.
edit:
1st subset: month, last_day 2nd subset: month, last_average select last_day , last_average ( subset1 ) left bring together ( subset2 ) using (month)
sql oracle select oracle-sqldeveloper
Comments
Post a Comment