0

我有一个大型数据表,它是由队长将数据输入到电子表格中创建的,它基本上是我们当地羽毛球联赛的记分表。表示每个匹配项,并且该匹配项的所有数据都包含在该行中。例如日期、分区、主队、客队、球员、合作伙伴、得分等。

我试图从我的数据中得到的是最成功的合作伙伴关系。我已经开始使用以下方法识别所有不同的合作伙伴关系:

(SELECT "home mens 1" AS "mens" FROM "Results") UNION
(SELECT "home mens 2" AS "mens" FROM "Results") UNION
(SELECT "home mens 3" AS "mens" FROM "Results") UNION
(SELECT "away mens 1" AS "mens" FROM "Results") UNION
(SELECT "away mens 2" AS "mens" FROM "Results") UNION
(SELECT "away mens 3" AS "mens" FROM "Results") ORDER BY "mens"

这给了我一个很好的配对清单。您可以在此处查看数据表,列标题:https ://docs.google.com/spreadsheet/pub?key=0AkFtD3i39wUZdFJSbVVXUUVvNi0wSXNmNEJ5XzhCWEE&output=html

如果不可能,或者如果我应该以某种方式重组数据,那么我很想听听替代解决方案。

我试过了:

(SELECT "home mens 1" AS "mens", SUM("home score game 1" + "home score game 2") AS "points total" FROM "Results") UNION
(SELECT "home mens 2" AS "mens", SUM("home score game 5" + "home score game 6") AS     "points total" FROM "Results") UNION
(SELECT "home mens 3" AS "mens", SUM("home score game 9" + "home score game 10") AS "points total" FROM "Results") UNION
(SELECT "away mens 1" AS "mens", SUM("away score game 1" + "away score game 2") AS "points total" FROM "Results") UNION
(SELECT "away mens 2" AS "mens", SUM("away score game 5" + "away score game 6") AS "points total" FROM "Results") UNION
(SELECT "away mens 3" AS "mens", SUM("away score game 9" + "away score game 10") AS "points total" FROM "Results") ORDER BY "mens"`

但似乎根本不起作用..

在伙伴关系与游戏分数的关系方面:与配对相关的分数列如下:

  • 男子对 1 玩游戏 1 和 2
  • 女士对 1 玩游戏 3 和 4
  • 男双 2 玩游戏 5 和 6
  • 女士对 2 玩游戏 7 和 8
  • 男子双人 3 玩游戏 9 和 10
  • 女士对 3 玩游戏 11 和 12
  • 混合对 1 玩游戏 13 和 14
  • 混合双人玩游戏 15 和 16
  • 混合对 3 玩游戏 17 和 18。

最成功的一对将是赢得最多比赛(得分为 15)的一对,然后是每场比赛平均得分最高的一对。

我在 OpenOffice Base 中执行此操作,虽然就 SQL 而言,我不确定这是基于什么 - 显然这意味着它正在使用 HSQL:http ://www.oooforum.org/forum/viewtopic.phtml?t=140063

4

2 回答 2

1

假设列的内容是您所说的“伙伴关系”,那么您就在正确的轨道上。您需要对您指定的查询进行聚合、计数和排序。此外,您想使用union all而不是union, 因为union删除重复项 - 这是您想要找到的:

select "mens", count(*) as cnt
from ((SELECT "home mens 1" AS "mens" FROM "Results") UNION all
      (SELECT "home mens 2" AS "mens" FROM "Results") UNION all
      (SELECT "home mens 3" AS "mens" FROM "Results") UNION all
      (SELECT "away mens 1" AS "mens" FROM "Results") UNION all
      (SELECT "away mens 2" AS "mens" FROM "Results") UNION all
      (SELECT "away mens 3" AS "mens" FROM "Results")
     ) t
group by "mens"
order by count(*) desc

这也许只是一个开始。您需要处理拼写差异,并且成功的定义可能不同。

于 2013-04-04T00:38:17.720 回答
0

我认为您的数据遗漏了一些东西,这导致您无法准确找到“最成功的合作伙伴”。你有所有的合伙人,所有的比赛得分,但是哪个合伙人得到哪个比赛得分和多少得分之间没有关系。否则,不难发现你想要什么。

如何找到最成功的主队合作伙伴关系的一个示例

注意:假设 RDBMS 是 Oracle,未经测试的代码


SELECT A.Partner, 
       SUM(CASE WHEN "GameScore" = 15 THEN 1 ELSE 0 END) AS "NumberOfWin", 
       AVG("GameScore") as "AvgGameScore"
FROM (
SELECT "home mens 1" AS "Partner", "home score game 1"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mens 1" AS "Partner", "home score game 2"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home ladies 1" AS "Partner", "home score game 3"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home ladies 1" AS "Partner", "home score game 4"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mens 2" AS "Partner", "home score game 5"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mens 2" AS "Partner", "home score game 6"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home ladies 2" AS "Partner", "home score game 7"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home ladies 2" AS "Partner", "home score game 8"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mens 3" AS "Partner", "home score game 9"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mens 3" AS "Partner", "home score game 10"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home ladies 3" AS "Partner", "home score game 11"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home ladies 3" AS "Partner", "home score game 12"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mixed 1" AS "Partner", "home score game 13"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mixed 1" AS "Partner", "home score game 14"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mixed 2" AS "Partner", "home score game 15"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mixed 2" AS "Partner", "home score game 16"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mixed 3" AS "Partner", "home score game 17"  AS  "GameScore" FROM "Results"  UNION ALL
SELECT "home mixed 3" AS "Partner", "home score game 18"  AS  "GameScore" FROM "Results"  
) A
GROUP BY A.Partner
ORDER BY SUM(CASE WHEN "GameScore" = 15 THEN 1 ELSE 0 END), AVG("GameScore")
于 2013-04-04T00:20:58.930 回答