如何在一列中找到小于postgresql中特定值的总值?
我有一个类似这样的专栏如何在一列中找到小于postgresql中特定值的总值?,postgresql,Postgresql,我有一个类似这样的专栏 Quantity 20 40 10 25 我需要得到该列中小于特定值的值的总数,如 Quantity Value 20 1 40 3 10 0 25 2 使用小于当前值的值将表连接到自身: select a.quantity, count(distinct b.id) from mytable a left join mytable b on b.quantity < a.quanti
Quantity
20
40
10
25
我需要得到该列中小于特定值的值的总数,如
Quantity Value
20 1
40 3
10 0
25 2
使用小于当前值的值将表连接到自身:
select a.quantity, count(distinct b.id)
from mytable a
left join mytable b on b.quantity < a.quantity
group by a.quantity
选择countdistinct b.id处理非唯一数量和没有行可连接的最低值,因此连接将返回null,该计数不计算