找到2个最接近的对应行,以达到乘以乘以的平均值,当给出输入标题时由匹配标题分组

发布于 2025-02-08 06:08:41 字数 4699 浏览 4 评论 0原文

我想找到由匹配标题分组的乘以值的总和的平均值,以便给出相应的评分,然后找到最接近输入值的2。

SELECT titleValueAVG / 3 AS average,
       title
  FROM (
           SELECT Sum(a) AS titleValueAVG,
                  title
             FROM (
                      SELECT value * 1 AS a,
                             title
                        FROM Table1
                       WHERE type = 'A' AND 
                             contesting = 'yes'
                      UNION ALL
                      SELECT value * 2,
                             title
                        FROM Table1
                       WHERE type = 'A' AND 
                             contesting = 'no'
                      UNION ALL
                      SELECT value * 3,
                             title
                        FROM Table1
                       WHERE type = 'A' AND 
                             contesting = 'undecided'
                  )
            GROUP BY title
            ORDER BY title = 'Australia' DESC,
                     ABS(titleValueAVG) - (
                                        SELECT value * 1 AS a,
                                               title
                                          FROM Table1
                                         WHERE type = 'A' AND 
                                               contesting = 'yes' AND title = 'Australia'
                                        UNION ALL
                                        SELECT value * 2,
                                               title
                                          FROM Table1
                                         WHERE type = 'A' AND 
                                               contesting = 'no' AND title = 'Australia'
                                        UNION ALL
                                        SELECT value * 3,
                                               title
                                          FROM Table1
                                         WHERE type = 'A' AND 
                                               contesting = 'undecided' AND title = 'Australia'
                                    )
       ) limit 2;

从示例表中:

| Title    | Type         | Competing      | Value          |

| -------- | -------------| -------------- | -------------- |

| Australia| A            | yes            | 26             |

| Australia| A            | no             | 162            |

| Australia| A            | undecided      | 37             |

| Spain    | A            | yes            | 14             |

| Spain    | A            | no             | 101            |

| Spain    | A            | undecided      | 11             |

| Ireland  | A            | yes            | 124            |

| Ireland  | A            | no             | 62             |

| Ireland  | A            | undecided      | 9              |

| Nigeria  | C            | yes            | 4              |

| Nigeria  | C            | no             | 11             |

| Nigeria  | C            | undecided      | 7              |

| Colombia | A            | yes            | 26             |

| Colombia | A            | no             | 12             |

| Colombia | A            | undecided      | 19             |

| Turkey   | A            | yes            | 29             |

| Turkey   | A            | no             | 145            |

| Turkey   | A            | undecided      | 24             |

| Malta    | B            | yes            | 1              |

| Malta    | B            | no             | 11             |

| Malta    | B            | undecided      | 4              |

| Mexico   | A            | yes            | 74             |

| Mexico   | A            | no             | 19             |

| Mexico   | A            | undecided      | 12             |

| Slovenia | B            | yes            | 16             |

| Slovenia | B            | no             | 22             |

| Slovenia | B            | undecided      | 11             |

| Canada   | A            | yes            | 29             |

| Canada   | A            | no             | 164            |

| Canada   | A            | undecided      | 40             |

| Kenya    | C            | yes            | 8              |

| Kenya    | C            | no             | 12             |

| Kenya    | C            | undecided      | 0              |

因此,在此示例中,我想从输入标题的“澳大利亚:

| Title    | average        |

| -------- | -------------- |

| Australia| 154            |

| Canada   | 159            |

| Turkey   | 130            |

我的尝试解决方案”中返回,我尝试了多种方法来组织订单,这就是我认为的问题,但是我根本无法工作。如果有人可以帮助我解决这个问题,我将非常感谢。

I want to find the average values of a sum of multiplied values grouped by a matching title, in order to give them a corresponding rating, and then find the 2 closest to the input value.

SELECT titleValueAVG / 3 AS average,
       title
  FROM (
           SELECT Sum(a) AS titleValueAVG,
                  title
             FROM (
                      SELECT value * 1 AS a,
                             title
                        FROM Table1
                       WHERE type = 'A' AND 
                             contesting = 'yes'
                      UNION ALL
                      SELECT value * 2,
                             title
                        FROM Table1
                       WHERE type = 'A' AND 
                             contesting = 'no'
                      UNION ALL
                      SELECT value * 3,
                             title
                        FROM Table1
                       WHERE type = 'A' AND 
                             contesting = 'undecided'
                  )
            GROUP BY title
            ORDER BY title = 'Australia' DESC,
                     ABS(titleValueAVG) - (
                                        SELECT value * 1 AS a,
                                               title
                                          FROM Table1
                                         WHERE type = 'A' AND 
                                               contesting = 'yes' AND title = 'Australia'
                                        UNION ALL
                                        SELECT value * 2,
                                               title
                                          FROM Table1
                                         WHERE type = 'A' AND 
                                               contesting = 'no' AND title = 'Australia'
                                        UNION ALL
                                        SELECT value * 3,
                                               title
                                          FROM Table1
                                         WHERE type = 'A' AND 
                                               contesting = 'undecided' AND title = 'Australia'
                                    )
       ) limit 2;

From an example table:

| Title    | Type         | Competing      | Value          |

| -------- | -------------| -------------- | -------------- |

| Australia| A            | yes            | 26             |

| Australia| A            | no             | 162            |

| Australia| A            | undecided      | 37             |

| Spain    | A            | yes            | 14             |

| Spain    | A            | no             | 101            |

| Spain    | A            | undecided      | 11             |

| Ireland  | A            | yes            | 124            |

| Ireland  | A            | no             | 62             |

| Ireland  | A            | undecided      | 9              |

| Nigeria  | C            | yes            | 4              |

| Nigeria  | C            | no             | 11             |

| Nigeria  | C            | undecided      | 7              |

| Colombia | A            | yes            | 26             |

| Colombia | A            | no             | 12             |

| Colombia | A            | undecided      | 19             |

| Turkey   | A            | yes            | 29             |

| Turkey   | A            | no             | 145            |

| Turkey   | A            | undecided      | 24             |

| Malta    | B            | yes            | 1              |

| Malta    | B            | no             | 11             |

| Malta    | B            | undecided      | 4              |

| Mexico   | A            | yes            | 74             |

| Mexico   | A            | no             | 19             |

| Mexico   | A            | undecided      | 12             |

| Slovenia | B            | yes            | 16             |

| Slovenia | B            | no             | 22             |

| Slovenia | B            | undecided      | 11             |

| Canada   | A            | yes            | 29             |

| Canada   | A            | no             | 164            |

| Canada   | A            | undecided      | 40             |

| Kenya    | C            | yes            | 8              |

| Kenya    | C            | no             | 12             |

| Kenya    | C            | undecided      | 0              |

So, in this example, I would like to return from an input title 'Australia:

| Title    | average        |

| -------- | -------------- |

| Australia| 154            |

| Canada   | 159            |

| Turkey   | 130            |

My attempted solution is above, I've tried multiple ways to organise the order by, which is what I think is the issue, but I can't get it to work at all. If anyone could help me fix this I'd really appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2025-02-15 06:08:42

使用条件聚合:

WITH cte AS (
  SELECT title,
         ROUND(SUM(value *
             CASE competing
               WHEN 'yes' THEN 1
               WHEN 'no' THEN 2
               WHEN 'undecided' THEN 3
             END
         ) / 3.0) average
  FROM tablename
  WHERE type = 'A'
  GROUP BY title
)
SELECT *
FROM cte
ORDER BY title = 'Australia' DESC,
         ABS(average - (SELECT average FROM cte WHERE title = 'Australia'))
LIMIT 3; 

请参阅

Use conditional aggregation:

WITH cte AS (
  SELECT title,
         ROUND(SUM(value *
             CASE competing
               WHEN 'yes' THEN 1
               WHEN 'no' THEN 2
               WHEN 'undecided' THEN 3
             END
         ) / 3.0) average
  FROM tablename
  WHERE type = 'A'
  GROUP BY title
)
SELECT *
FROM cte
ORDER BY title = 'Australia' DESC,
         ABS(average - (SELECT average FROM cte WHERE title = 'Australia'))
LIMIT 3; 

See the demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文