如何连续统计SQL中所有大于或等于自身的?

How to consecutively count everything greater than or equal to itself in SQL?

假设我有一个 table 包含每个设备类型和设备年龄的设备的设备 ID,我如何做一个 Count Distinct 至少具有该设备年龄的设备 ID .

例如,假设这是我们拥有的所有数据:

equipment_type equipment_id equipment_age
Screwdriver A123 1
Screwdriver A234 2
Screwdriver A345 2
Screwdriver A456 2
Screwdriver A567 3

我希望输出为:

equipment_type equipment_age count_of_equipment_at_least_this_age
Screwdriver 1 5
Screwdriver 2 4
Screwdriver 3 1

原因是有 5 把螺丝刀至少使用了 1 天,4 把螺丝刀至少使用了 2 天,只有 1 把螺丝刀至少使用了 3 天。

到目前为止,我只能计算属于每个 equipment_age 的设备数量(如下所示的查询),但不是“至少 equipment_age”。

SELECT
equipment_type,
equipment_age,
COUNT(DISTINCT equipment_id) as count_of_equipments
FROM equipment_table
GROUP BY 1, 2

使用自连接方法:

SELECT
    e1.equipment_type,
    e1.equipment_age,
    COUNT(*) AS count_of_equipments
FROM equipment_table e1
INNER JOIN equipment_table e2
    ON e2.equipment_type = e1.equipment_type AND
       e2.equipment_age >= e1.equipment_age
GROUP BY 1, 2
ORDER BY 1, 2;

GROUP BY 将 COUNT 的范围限制在组中的行,即它不会让您到达其他行(equipment_age 大于当前组的行)。所以你需要一个子查询或窗口函数来获取它们。一种方式:

SELECT
   equipment_type,
   equipment_age,
   (Select COUNT(*) 
    from equipment_table cnt 
     where cnt.equipment_type = a.equipment_type
       AND cnt.equipment_age >= a.equipment_age
     )  as count_of_equipments
FROM equipment_table a
GROUP BY 1, 2, 3

不过,我不确定您的环境是否支持这种语法。如果没有,请告诉我们我们会找到其他方法。

考虑以下join-less解决方案

select distinct
  equipment_type,
  equipment_age,
  count(*) over equipment_at_least_this_age as count_of_equipment_at_least_this_age
from equipment_table 
window equipment_at_least_this_age as (
  partition by equipment_type 
  order by equipment_age 
  range between current row and unbounded following
)     

如果应用于您问题中的示例数据 - 输出为