2016-11-10 71 views
0

我有一個存儲高爾夫相關數據的數據庫。我試圖計算每個球場上每個球洞的鷹,小鳥,杆等的數量,並將數量插入洞表中的相應屬性。Oracle PL/SQL將查詢結果合併到表列中

我可以編寫查詢來提取我想要的信息,但我不確定如何查看我的查詢結果並將其合併到我的表Hole中的相應記錄中。我已經查看了Oracle SQL的MERGE文檔,但沒有取得任何成功。

這就是我現在所擁有的:

--Count all the birdies on holes 1-18 of course 538 
select phs.course_id, phs.hole_num, count(*) from player_hole_score phs 
join hole h on 
    phs.hole_num = h.hole_num and 
    phs.course_id = h.course_id 
    where phs.score = h.hole_par - 1 and phs.course_id = 538 
    group by phs.hole_num, phs.course_id 
    order by phs.course_id, phs.hole_num; 

--Where the data needs to be inserted 
select course_id, hole_num, hole_num_birdie from hole 
where course_id = 538; 

兩個查詢結果如下:

  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 |     | 
+-----------+----------+----------+  +-----------+----------+-----------------+ 

我如何可以從第一個查詢結果列COUNT(*),並使用計數更新表中對應的記錄Hole,以便得到如下結果:

+-----------+----------+-----------------+ 
| 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 | 
+-----------+----------+-----------------+ 

編輯:After聽到它使用視圖聽起來像是最好的方法來解決這個問題。我能夠使用mathguy的代碼將它合併到現有的表中,但我不確定如何將該代碼轉換爲視圖。特別是,我不能爲我的子查詢分配一個別名的事實正在拋棄我。

我有,工程合併此代碼:

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

我認爲創建視圖將是類似的,但我已經把現在是給我0的結果。我需要在下面更改什麼?

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

這並不難,但你是否確定要這樣做?每天都有更多需要記錄的事件,你是否會一直更新表格?正常(和高效)的解決方案是編寫一個VIEW,它將即時顯示所有這些統計數據,並始終保持最新狀態。如果性能很重要,則可以「實現」該視圖,以便對其進行快速查詢。 – mathguy

+0

@mathguy這是一個學校項目,我已經收集了所有的數據,所以我不關心更多的事件被記錄。我想要一份更新聲明,以便我可以更新所有數量,而無需執行其他任何操作。 –

回答

1
merge into hole 
    using ( your query here ) q 
    on (hole.course_id = q.course_id and hole.hole_num = q.hole_num) 
when matched 
    then update set hole.hole_num_birdie = q.ct 
where hole.course_id = 538 -- this is optional, you can update all at once 

your query here是第一個查詢,減去其不需要ORDER BY條款。請注意,在MERGE聲明中給出了別名q

在第一個查詢中,您需要爲count(*)列提供別名:count(*) as ct

在你做這件事之前,請考慮一下我在你評論的原文中所說的話。

+0

感謝使用合併的代碼,這也正是我需要它的原因。我編輯了上面的帖子,使用視圖來做同樣的事情。你知道我在上面的嘗試中需要修改什麼嗎? –

+0

@EricPratt - 我不知道我理解你的問題。視圖創建應該非常簡單 - 「CREATE VIEW AS SELECT .......'(查詢的其餘部分)。您可能不需要WHERE子句,在創建視圖時將其保留在查詢之外。然後,當你需要查詢這個視圖時,你可以說「select ... from WHERE ....」,並將course_id和hole_id添加到該查詢中。 – mathguy

0

如果性能不是問題,我會使用視圖而不是插入到表中。