我已经设法按月获得累积的不同计数,但在尝试按年计算时却无法获得。我尝试了分区功能,但我不断收到各种错误,这就是我到目前为止所得到的。你能给我一些指导吗,我是新手。
[SQL 小提琴][1]
PostgreSQL 9.3 模式设置:
create table table2 (id serial, fecha_captura date, mn integer, VALUACION1 integer);
insert into table2 (fecha_captura, mn, VALUACION1)
values
('1996-02-01',2,12345),
('1996-02-01',2,12345),
('1996-02-01',2,12344),
('1996-02-01',2,12343),
('1996-03-01',3,12345),
('1996-03-01',3,12346),
('1996-03-01',3,12347),
('1996-03-01',3,12348),
('1996-04-01',4,12349),
('1996-04-01',4,12350),
('1996-04-01',4,12351),
('1996-04-01',4,12352),
('1997-03-01',4,12353),
('1997-03-01',4,12354),
('1997-03-01',4,12345),
('1997-03-01',4,12352),
('1997-04-01',4,12353),
('1997-04-01',4,12354),
('1997-04-01',4,12345),
('1997-04-01',4,12355);
查询 1:
SELECT Year1,mn,
COUNT(DISTINCT(VALUACION1)) AS Val_Dist_Mes
,SUM(flag) AS Val_Dist_Mes_Nuevas
,SUM(SUM(flag))
OVER (partition by Year1 ORDER BY mn,Year1
ROWS UNBOUNDED PRECEDING) AS Val_Dist_Acum
FROM
(
SELECT
VALUACION1
,EXTRACT(MONTH FROM fecha_captura) AS mn
,EXTRACT(YEAR FROM fecha_captura) AS Year1
,CASE
WHEN EXTRACT(MONTH FROM fecha_captura)
= MIN (EXTRACT(MONTH FROM fecha_captura))
OVER (PARTITION BY VALUACION1)
THEN 1
ELSE 0
END AS flag
FROM table2
GROUP BY fecha_captura,VALUACION1
) AS dt
GROUP BY mn,Year1
[结果][2]:
| year1 | mn | val_dist_mes | val_dist_mes_nuevas | val_dist_acum |
|-------|----|--------------|---------------------|---------------|
| 1996 | 2 | 3 | 3 | 3 |
| 1996 | 3 | 4 | 3 | 6 |
| 1996 | 4 | 4 | 3 | 9 |
| 1997 | 3 | 4 | 3 | 3 |
| 1997 | 4 | 4 | 1 | 4 |
[预期结果][2]:
| year1 | mn | val_dist_mes | val_dist_mes_nuevas | val_dist_acum |
|-------|----|--------------|---------------------|----------------|
| 1996 | 2 | 3 | 3 | 3 |
| 1996 | 3 | 4 | 3 | 6 |
| 1996 | 4 | 4 | 4 | 10 |
| 1997 | 3 | 4 | 4 | 4 |
| 1997 | 4 | 4 | 1 | 5 |