SQL中计算值大于或等于另一列值的行数

发布于 2024-12-21 11:06:23 字数 1361 浏览 4 评论 0原文

我有一个包含两列的表:几个 id 和一些“标记” 夫妻。我想要一个结果,其中列出有 x 标记的情侣数量 x 的每个值或更多。所以我的输入看起来像:

| couple_id | num_marks |
|-----------+-----------|
|         9 |         7 |
|         6 |         6 |
|         8 |         6 |
|         2 |         5 |
|         3 |         4 |
|         5 |         4 |
|         1 |         3 |
|         4 |         3 |
|        10 |         2 |
|         7 |         1 |

我想得到结果:

| num_marks | num_couples |
|-----------+-------------|
|         7 | 1           |
|         6 | 3           |
|         5 | 4           |
|         4 | 6           |
|         3 | 8           |
|         2 | 9           |
|         1 | 10          |

即有 1 对有 7 个或更多标记,3 对有 6 个或更多标记,4 拥有 5 分或更多分数的情侣等。我已经能够提出一个查询 返回恰好 n 个标记的情侣数量:

SELECT num_marks,
       count(couple_id) AS num_couples
  FROM table_name
  GROUP BY num_marks
  ORDER BY num_marks DESC;

其结果是:

| num_marks | num_couples |
|-----------+-------------|
|         7 |           1 |
|         6 |           2 |
|         5 |           1 |
|         4 |           2 |
|         3 |           2 |
|         2 |           1 |
|         1 |           1 |

即有 1 对有 7 个标记,2 对有 6 个标记,1 个有 5 个标记,等等。 有一种方便的方法可以有效地将每行的值与上面的值相加 它?我可以在应用程序级别做到这一点,但这似乎是这样的事情 它真正属于数据库。

I have a table with two columns: a couple id and a number of "marks" for that
couple. I'd like a result which lists the number of couples which have x marks
or more for each of the values of x. So my input looks like:

| couple_id | num_marks |
|-----------+-----------|
|         9 |         7 |
|         6 |         6 |
|         8 |         6 |
|         2 |         5 |
|         3 |         4 |
|         5 |         4 |
|         1 |         3 |
|         4 |         3 |
|        10 |         2 |
|         7 |         1 |

And I'd like to get the result:

| num_marks | num_couples |
|-----------+-------------|
|         7 | 1           |
|         6 | 3           |
|         5 | 4           |
|         4 | 6           |
|         3 | 8           |
|         2 | 9           |
|         1 | 10          |

I.e. there was 1 couple with 7 or more marks, 3 couples with 6 or more marks, 4
couples with 5 or more marks, etc. I've been able to come up with a query to
return the number of couples with exactly n marks:

SELECT num_marks,
       count(couple_id) AS num_couples
  FROM table_name
  GROUP BY num_marks
  ORDER BY num_marks DESC;

Which yields:

| num_marks | num_couples |
|-----------+-------------|
|         7 |           1 |
|         6 |           2 |
|         5 |           1 |
|         4 |           2 |
|         3 |           2 |
|         2 |           1 |
|         1 |           1 |

I.e. there was 1 couple with 7 marks, 2 couples with 6 marks, 1 with 5, etc. Is
there a convenient way effectively to sum the value of each row with those above
it? I can do it at the application level, but it seems like the kind of thing
which really belongs in the database.

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

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

发布评论

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

评论(2

怪我鬧 2024-12-28 11:06:23

这可能不是特别有效,但应该可以完成工作:

SELECT t1.num_marks,       
  (SELECT count(t2.couple_id)
   FROM table_name t2  
   WHERE t2.num_marks >= t1.num_marks
   ) AS num_couples 
FROM table_name t1  
GROUP BY t1.num_marks   
ORDER BY t1.num_marks DESC;

编辑:
您可以在 select、from、where 组中使用子查询 by 和having 查询的子句,如果您引用主/外部“查询”,那么它将评估每一行的子查询,那么它被称为 相关子查询。 (因此对性能的警告)

根据 Damien 的回答,您还可以使用 CTE - CTE 可以提高可读性,并且还可以使递归和自连接变得更容易(IMO)。

据我所知,大多数 SQL 都支持子查询。

This might not be particularly efficient but should get the job done:

SELECT t1.num_marks,       
  (SELECT count(t2.couple_id)
   FROM table_name t2  
   WHERE t2.num_marks >= t1.num_marks
   ) AS num_couples 
FROM table_name t1  
GROUP BY t1.num_marks   
ORDER BY t1.num_marks DESC;

Edit :
You can use a sub query in the select, from, where, group by and having clauses of a query, and if you reference the main / outer 'query' then it will evaluate the subquery for each row, then it is known as a correlated subquery. (Hence the caveat about performance)

As per Damien's answer, you could also use a CTE - CTE's can improve readability and also make recursion and self-joins a lot easier IMO.

AFAIK subqueries are supported in most SQL's.

伤感在游骋 2024-12-28 11:06:23

您可以使用 RANK() 函数来计算每个结果的排名,然后只需将并列结果的数量添加到该排名上即可:

create table #T (couple_id int,num_marks int)
insert into #T (couple_id,num_marks)
select    9 ,         7 union all
select    6 ,         6 union all
select    8 ,         6 union all
select    2 ,         5 union all
select    3 ,         4 union all
select    5 ,         4 union all
select    1 ,         3 union all
select    4 ,         3 union all
select   10 ,         2 union all
select    7 ,         1

;with Ranked as (
    select num_marks,RANK() OVER (ORDER BY num_marks desc) as rk from #T
)
select num_marks,rk + COUNT(*) -1 as Result from Ranked
group by num_marks,rk

给出:(

num_marks   Result
----------- --------------------
7           1
6           3
5           4
4           6
3           8
2           9
1           10

(7 row(s) affected)

当然,如果您需要按特定顺序排列结果,请不要忘记添加 ORDER BY 子句 - 上述排序只是一个快乐 事故)

You can use the RANK() function to work out where each result ranks, then just add the number of tied results onto that rank:

create table #T (couple_id int,num_marks int)
insert into #T (couple_id,num_marks)
select    9 ,         7 union all
select    6 ,         6 union all
select    8 ,         6 union all
select    2 ,         5 union all
select    3 ,         4 union all
select    5 ,         4 union all
select    1 ,         3 union all
select    4 ,         3 union all
select   10 ,         2 union all
select    7 ,         1

;with Ranked as (
    select num_marks,RANK() OVER (ORDER BY num_marks desc) as rk from #T
)
select num_marks,rk + COUNT(*) -1 as Result from Ranked
group by num_marks,rk

Gives:

num_marks   Result
----------- --------------------
7           1
6           3
5           4
4           6
3           8
2           9
1           10

(7 row(s) affected)

(Of course, if you need the results in a particular order, don't forget to add an ORDER BY clause - the above ordering is just a happy accident)

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