2016-11-10 72 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

如果性能不是问题,我会使用视图而不是插入到表中。