我有下表,我想计算每个名字和每个标志的连续日期。
Name Date Flag
-------------------------------------
Alberta 01-01-2018 B
Alberta 02-01-2018 B
Alberta 03-01-2018 B
Alberta 04-01-2018 L
Alberta 05-01-2018 L
Ambelos 01-01-2018 B
Ambelos 02-01-2018 B
Ambelos 03-01-2018 L
Ambelos 04-01-2018 B
我想要下面的
Name Date Flag CountDays
----------------------------------------------------
Alberta 03-01-2018 B 3
Alberta 05-01-2018 L 2
Ambelos 02-01-2018 B 2
Ambelos 03-01-2018 L 1
Ambelos 04-01-2018 B 1
我该怎么做?
答案 0 :(得分:0)
您可以使用TABIBITOSAN技术
PostgreSQL 9.6架构设置:
CREATE TABLE t
(Name varchar(7), dt timestamp, Flag varchar(1))
;
INSERT INTO t
(Name, dt, Flag)
VALUES
('Alberta', '2018-01-01 00:00:00', 'B'),
('Alberta', '2018-02-01 00:00:00', 'B'),
('Alberta', '2018-03-01 00:00:00', 'B'),
('Alberta', '2018-04-01 00:00:00', 'L'),
('Alberta', '2018-05-01 00:00:00', 'L'),
('Ambelos', '2018-01-01 00:00:00', 'B'),
('Ambelos', '2018-02-01 00:00:00', 'B'),
('Ambelos', '2018-03-01 00:00:00', 'L'),
('Ambelos', '2018-04-01 00:00:00', 'B')
;
查询1 :
SELECT name,
Max(dt) AS "Date",
flag,
Count(*) As CountDays
FROM (SELECT t.*,
row_number()
over (
PARTITION BY name
ORDER BY dt ) - row_number()
over (
PARTITION BY name, flag
ORDER BY dt ) AS seq
FROM t) s
GROUP BY name,
flag,
seq
ORDER BY name,"Date"
<强> Results 强>:
| name | Date | flag | countdays |
|---------|----------------------|------|-----------|
| Alberta | 2018-03-01T00:00:00Z | B | 3 |
| Alberta | 2018-05-01T00:00:00Z | L | 2 |
| Ambelos | 2018-02-01T00:00:00Z | B | 2 |
| Ambelos | 2018-03-01T00:00:00Z | L | 1 |
| Ambelos | 2018-04-01T00:00:00Z | B | 1 |