我需要计算满足某些条件的行数,这些行包含在满足其他条件的其他行定义的间隔中。示例:'参考'之间的行数N.值1和4满足条件' Other_condition' = b是N = 1,'参考'之间的行数N.具有满足条件的值2和5' Other_condition' = b是N = 2等。
Date Reference Other_condition
20171111 1 a
20171112 2 a
20171113 3 b
20171114 4 b
20171115 5 b
我通过Hive / Impala SQL查询访问数据库,遗憾的是我不知道从哪里开始实现这样的窗口函数。我想要的半伪代码版本将是:
SELECT COUNT (DISTINCT database.Date) AS counter, Other_condition, reference
FROM database
WHERE database.Other_condition = a AND database.Reference BETWEEN
(window function condition 1: database.Reference = 2) AND
(window function condition 2: database.Reference = 5)
GROUP BY counter
答案 0 :(得分:1)
你的问题很难理解。我得到了第一个条件,即" 1"之间的行数。和" 4"。
这是一个非常容易概括的方法:
select (max(case when reference = 4 then seqnum end) -
max(case when reference = 1 then seqnum end)
) as num_rows_1_4
from (select t.*,
row_number() over (order by date) as seqnum
from t
) t;