我有一个存储高尔夫相关数据的数据库。我试图计算每个球场上每个球洞的鹰,小鸟,球等的数量,并将数量插入球洞表中的相应属性。
我可以编写查询来提取我喜欢的信息,但我不确定如何获取查询结果并将其合并到我的表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 |
+-----------+----------+-----------------+
编辑:听到评论之后听起来像使用视图是解决这个问题的最佳方法。我能够使用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 :(得分: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
子句。请注意,q
语句中的别名为MERGE
。
在第一个查询中,您需要为count(*)
列提供别名:count(*) as ct
。
在此之前,请考虑我在原始帖子下的评论中所说的内容。
答案 1 :(得分:0)
如果性能不是问题,我会使用视图而不是插入表格。