[英]PostgreSQL group by greater than equal to
I am trying to find the count over a table (R1) based on whether or not it's value that is greater than or equal to values in another table (R2), grouping based on the values in R2.我试图根据它的值是否大于或等于另一个表 (R2) 中的值来查找表 (R1) 上的计数,并根据 R2 中的值进行分组。 This would end up having a sum of counts greater than the total number of entries in the table, since some entries could be greater than or equal to multiple values.这将导致计数总和大于表中条目的总数,因为某些条目可能大于或等于多个值。
For example,例如,
R1 R1
a一个 | b b |
---|---|
a一个 | 1 1 |
b b | 2 2 |
a一个 | 3 3 |
c c | 2 2 |
a一个 | 4 4 |
f F | 1 1 |
c c | 3 3 |
g G | 4 4 |
R2 R2
val值 |
---|
1 1 |
2 2 |
3 3 |
The ideal result would look like理想的结果看起来像
val值 | count_gte count_gte |
---|---|
1 1 | 8 8 |
2 2 | 6 6 |
3 3 | 4 4 |
Since 1
has 8 values that appear that are greater than or equal, 2
has 6 values that appear that are greater than or equal, and 3
has 4 values that are greater than or equal.由于1
有 8 个出现大于等于的值, 2
有 6 个出现大于等于的值, 3
有 4 个出现大于等于的值。
Thanks for any help you can offer谢谢你尽你所能的帮助
select r2.val, (select count(*) from r1 where r1.b>=r2.val) as count_gte from r2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.