Oracle PL/SQL Merge Query Result into Table Column -


i have database stores golf related data. i'm trying count number of eagles, birdies, pars etc each hole on each course , insert quantity corresponding attribute in hole table.

i can write queries pull information i'd like, i'm not sure how take query result , merge corresponding records within table hole. i've looked @ documentation oracle sql's merge, haven't had success.

here's have right now:

--count birdies on holes 1-18 of course 538 select phs.course_id, phs.hole_num, count(*) player_hole_score phs join hole h on    phs.hole_num = h.hole_num ,   phs.course_id = h.course_id   phs.score = h.hole_par - 1 , phs.course_id = 538   group phs.hole_num, phs.course_id   order phs.course_id, phs.hole_num;  --where data needs inserted select course_id, hole_num, hole_num_birdie hole course_id = 538; 

both query results below:

            query 1                                     query 2 (table hole) +-----------+----------+----------+     +-----------+----------+-----------------+ | course_id | hole_num | count(*) |     | course_id | hole_num | hole_num_birdie | +-----------+----------+----------+     +-----------+----------+-----------------+ |       538 |        1 |      103 |     |       538 |        1 |                 | |       538 |        2 |       76 |     |       538 |        2 |                 | |       538 |        3 |       42 |     |       538 |        3 |                 | |       538 |        4 |       71 |     |       538 |        4 |                 | |       538 |        5 |       82 |     |       538 |        5 |                 | |       538 |        6 |       77 |     |       538 |        6 |                 | |       538 |        7 |       90 |     |       538 |        7 |                 | |       538 |        8 |       34 |     |       538 |        8 |                 | |       538 |        9 |      188 |     |       538 |        9 |                 | |       538 |       10 |       87 |     |       538 |       10 |                 | |       538 |       11 |       53 |     |       538 |       11 |                 | |       538 |       12 |       95 |     |       538 |       12 |                 | |       538 |       13 |      137 |     |       538 |       13 |                 | |       538 |       14 |       69 |     |       538 |       14 |                 | |       538 |       15 |      170 |     |       538 |       15 |                 | |       538 |       16 |      197 |     |       538 |       16 |                 | |       538 |       17 |       56 |     |       538 |       17 |                 | |       538 |       18 |       82 |     |       538 |       18 |                 | +-----------+----------+----------+     +-----------+----------+-----------------+ 

how can take column count(*) first query result , use counts update corresponding records in table hole result this:

+-----------+----------+-----------------+ | course_id | hole_num | hole_num_birdie | +-----------+----------+-----------------+ |       538 |        1 |             103 | |       538 |        2 |              76 | |       538 |        3 |              42 | |       538 |        4 |              71 | |       538 |        5 |              82 | |       538 |        6 |              77 | |       538 |        7 |              90 | |       538 |        8 |              34 | |       538 |        9 |             188 | |       538 |       10 |              87 | |       538 |       11 |              53 | |       538 |       12 |              95 | |       538 |       13 |             137 | |       538 |       14 |              69 | |       538 |       15 |             170 | |       538 |       16 |             197 | |       538 |       17 |              56 | |       538 |       18 |              82 | +-----------+----------+-----------------+ 

edit: after hearing comments sounds using view best way approach this. able merge existing table using mathguy's code i'm not sure how transform code view. in particular, fact can't assign alias sub query throwing me off.

i have code merge works:

merge hole   using    (select phs.course_id, phs.hole_num, count(*) ct player_hole_score phs     join hole h on        phs.hole_num = h.hole_num ,       phs.course_id = h.course_id       phs.score = h.hole_par - 1       group phs.hole_num, phs.course_id)   q   on (hole.course_id = q.course_id , hole.hole_num = q.hole_num)   when matched      update set hole.hole_num_birdie = q.ct 

i thought create view similar have right giving me 0 results. need change below?

create view hole_statistic     select      hh.course_id,      hh.hole_num,       (select count(*) ct player_hole_score phs         join hole h on            phs.hole_num = h.hole_num ,           phs.course_id = h.course_id           phs.score = h.hole_par -1           group h.course_id, h.hole_num)         birdies      hole hh     group hh.course_id, hh.hole_num; 

merge hole   using (   query here   ) q   on (hole.course_id = q.course_id , hole.hole_num = q.hole_num) when matched   update set hole.hole_num_birdie = q.ct hole.course_id = 538  --  optional, can update @ once 

your query here first query, minus order by clause not needed. notice given alias q in merge statement.

in first query, need give alias count(*) column: count(*) ct.

before this, though, please consider said in comment under original post.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -