使用PostgreSQL 9.6。我想计算一列中两个其他值之间的值的次数。
在这个例子中,我正在搜索“mid”列,结果应该是5.如果我对值进行硬编码但是这是不可接受的。当使用字段值给出答案为20时,它会失败。它似乎完全忽略了低字段和高字段。
为什么在第二种情况声明中没有评估“低”和“高”字段?我该如何解决它?
SELECT low, mid, high,
sum(case when mid between 35 and 57 then 1 end) over(partition by mid between 35 and 57) as "StaticNums",
sum(case when mid between low and high then 1 end) over(partition by mid between low and high) as "non-StaticNums"
FROM
(SELECT l-11 as low, l as mid, l+11 as high
FROM generate_series(1,100,5) as l(n)
group by l.n
) as t
group by mid, low, high
order by mid
我添加了一张我正在寻找使用excel的结果的图片。简而言之,我希望每行计算给定范围内的小部件数量(使用低和高值以及小部件中间)。 但是我如何在sql中执行此操作?