date - PL/SQL find values within nested tables -
hi have problem don't know if can help. i've created nested tables , inserted values in in table.
create or replace type tt_hours object(hours integer, data number); / create or replace type tt_day varray(7) of tt_hours; / create table nem_rm16 ( day date, value_hours tt_day ); insert nem_rm16 (day, value_hours) values (to_date('01/06/2012 22:00:34'), tt_day( tt_hours(1,0.025727), tt_hours(2,0.012047), tt_hours(3,0.012857), tt_hours(4,0.012107), tt_hours(5,0.012849), tt_hours(6,0.01215), tt_hours(7,0.0129))); so there 30 rows inserted in new_table each representing 1 day of month in month (june). above example of first row day = 01/06/2012 8 hours of data.
how write program find average hour specific day e.g average of first hour of mondays in table (god hope i'm making sense).
the result should 7 rows (7 days in week)
tt_hours (1, average on month) tt_hours (2, average on month) .......... second table:
create table old_table ( tday date, value_thours tt_day ); i've tried insert using following. insert result in old table did not work.
insert old_table (day, value_hours) values (select to_char(day, 'day'), hours, avg(data) nem_rm16 n, table(n.value_hours) v group to_char(day, 'day'), hours); e.g old_table should have following data in friday after running select statement. hours , date in nested table , tdate date type. can see result select statement produced inserted same structured table.
tdate hours data ----------- ---------- ---------- friday 1 0,025727 friday 2 0,012047 friday 3 0,012857 friday 4 0,012107 friday 5 0,012849 friday 6 0,01215 friday 7 0,0129
you can use lateral join retrieve values nested tables:
sql> select n.day, v.hours, v.data nem_rm16 n, table(n.value_hours) v; day hours data ----------- ---------- ---------- 01/06/2012 1 0,025727 01/06/2012 2 0,012047 01/06/2012 3 0,012857 01/06/2012 4 0,012107 01/06/2012 5 0,012849 01/06/2012 6 0,01215 01/06/2012 7 0,0129 i'm not sure understand question correctly above query can run aggregation, such as:
sql> select to_char(day, 'day') day_of_week, hours, avg(data) avg_data 2 nem_rm16 n, table(n.value_hours) v 3 group to_char(day, 'day'), hours; day_of_week hours avg_data ----------- ---------- ---------- friday 1 0,025727 friday 2 0,012047 friday 3 0,012857 friday 4 0,012107 friday 5 0,012849 friday 6 0,01215 friday 7 0,0129 use collect if want reconstruct nested table, e.g:
select day_of_week, cast(collect(tt_hours(hours, avg_data)) tt_day) (select to_char(day, 'day') day_of_week, hours, avg(data) avg_data nem_rm16 n, table(n.value_hours) v group to_char(day, 'day'), hours) group day_of_week
Comments
Post a Comment