简体   繁体   English

归一化总和 (SQL) 的汇总列 - 第 1 部分

[英]Rollup Column for Normalized Sums (SQL) - Part 1

I have a table like so:我有一张这样的桌子:

object_id | vote
1 | 2
1 | -1
1 | 5
2 | 3
2 | 1
3 | 4
3 | -2

I want this result (for this particular example, object_ids 1 and 2 are part of a group, defined elsewhere, and I'm looking for the normalized_score so that the sum always = 1. object_id 3 is part of an unused group.):我想要这个结果(对于这个特定的示例,object_ids 1 和 2 是一个组的一部分,在其他地方定义,我正在寻找 normalized_score 以便总和始终 = 1。object_id 3 是未使用组的一部分。):

object_id | normalized_score
1 | 6/10
2 | 4/10

[added 3:05PM] 10 here is the sum of the votes for object_id in (1,2). [下午 3:05 添加] 10 这里是 (1,2) 中 object_id 的投票总和。 There's a whole other set of logic to come up with the (1,2), I was just trying to give the cleanest question so people don't have to worry about that part. (1,2)还有一整套逻辑,我只是想提出最简洁的问题,这样人们就不必担心那部分了。

[added 3:10PM] As pointed out in the comments, if the score for one of the objects is below 0, a problem arises. [下午 3:10 补充] 正如评论中所指出的,如果其中一个对象的分数低于 0,则会出现问题。 Here is the rule, "IF the score for any outcome_id is -x, AND that is the minimum score for the set, ADD x to all scores in order to zero-out the minimum score".这是规则,“如果任何结果 ID 的分数是 -x,并且这是集合的最低分数,则将 x 添加到所有分数以将最低分数归零”。 I can do this on my own time though outside of SQL - so it's a bonus only if somebody has the cahones to try to tackle it in SQL.尽管在 SQL 之外,我可以在自己的时间执行此操作 - 因此,只有当有人有能力尝试在 SQL 中解决它时,这才是一种奖励。

If I do a self join, I can get the sum.如果我进行自我加入,我可以获得总和。 I can't figure out how to get the normalized sum.我不知道如何获得归一化的总和。 Ideally this will work in both MySQL 5.x and Sqlite3.理想情况下,这适用于 MySQL 5.x 和 Sqlite3。 Otherwise, I can do this with two separate queries and just do the work in post-processing.否则,我可以使用两个单独的查询来执行此操作,然后在后处理中完成工作。

The comments are quite correct.. but I'll make the assumption that 10 is just some number you picked out of your... nose.评论是完全正确的.. 但我会假设 10 只是你从你的鼻子中挑选的一些数字。

SELECT object_id AS ObjectID, SUM(vote) + '/10' AS NormalizedVote FROM table GROUP BY object_id SELECT object_id AS ObjectID, SUM(vote) + '/10' AS NormalizedVote FROM table GROUP BY object_id

Enjoy.享受。

-- SQL solution
SELECT 
  object_id AS ObjectID, 
  (SUM(CASE SIGN(vote) WHEN 1 THEN vote ELSE 0 END) - 
    ((CASE SIGN(MIN(vote)) WHEN -1 THEN MIN(vote) ELSE 0) * 
     (COUNT(1)))) + '/10' AS NormalizedVote
FROM table 
GROUP BY object_id

The solution without compensating for negative votes (I include this one because its much easier to read/understand):不补偿反对票的解决方案(我包括这个是因为它更容易阅读/理解):

SELECT object_id
,      SUM(vote) + '/' + total AS normalized_score
FROM   tabel
,      (
       SELECT sum(vote) AS total
       FROM   tabel
       ) 
GROUP BY object_id, total

Full solution:完整解决方案:

SELECT object_id
,      SUM(vote + minvote) + '/' + (total + minvote * count) AS normalized_score
FROM   tabel
,      (
       SELECT sum(vote) AS total
       ,      CASE WHEN MIN(vote) < 0 THEN
                  -MIN(vote)
              END AS minvote
       ,      COUNT(*) AS count
       FROM   tabel
       ) 
GROUP BY object_id, total, minvote, count

(I don't have access to MySQL, so I wrote a query in Oracle and replaced || for +. Hope it works in MySQL or at least helps:)) (我无权访问 MySQL,所以我在 Oracle 中写了一个查询并替换为 || 为 +。希望它适用于 MySQL 或至少有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM