【发布时间】:2015-06-09 07:35:37
【问题描述】:
我的 Oracle 数据库中有以下结构:
Date Allocation id
2015-01-01 Same 200
2015-01-02 Good 200
2015-01-03 Same 200
2015-01-04 Same 200
2015-01-05 Same 200
2015-01-06 Good 200
我想要一个查询,它只需要检查前连续几天并获取分配为"Same" 的计数。
我想按日期选择,例如2015-01-05。
示例输出:对于日期2015-01-05,计数为3。
新问题。通过 Lukas Eder 的查询,计数始终为 1 或 2。但预期是3。为什么?!
Date Allocation id
2015-01-01 Same 400
2015-01-02 Good 400
2015-01-03 Same 400
2015-01-04 Same 400
2015-01-05 Same 400
2015-01-06 Good 400
来自 Lukas Eder 的代码
SELECT c
FROM (
SELECT allocation, d, count(*) OVER (PARTITION BY allocation, part ORDER BY d) AS c
FROM (
SELECT allocation, d,
d - row_number() OVER (PARTITION BY allocation ORDER BY d) AS part
FROM t
)
)
WHERE d = DATE '2015-01-05';
预期的输出是这样的,First_day end Last day 不需要:
id count first_day Last_Day
200 3 2015-01-03 2015-01-05
400 3 2015-01-03 2015-01-05
【问题讨论】:
-
您能告诉我们您尝试过的查询吗?
-
查看滞后和领先函数