我有一個存儲高爾夫相關數據的數據庫。我試圖計算每個球場上每個球洞的鷹,小鳥,杆等的數量,並將數量插入洞表中的相應屬性。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;
這並不難,但你是否確定要這樣做?每天都有更多需要記錄的事件,你是否會一直更新表格?正常(和高效)的解決方案是編寫一個VIEW,它將即時顯示所有這些統計數據,並始終保持最新狀態。如果性能很重要,則可以「實現」該視圖,以便對其進行快速查詢。 – mathguy
@mathguy這是一個學校項目,我已經收集了所有的數據,所以我不關心更多的事件被記錄。我想要一份更新聲明,以便我可以更新所有數量,而無需執行其他任何操作。 –