我已经设法按月计算累积的不同计数但是在尝试多年时我无法得到它。我尝试了分区功能,但我一直遇到各种错误,这是我到目前为止所得到的。你能给我一些指导吗,我是新人。
[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 |
答案 0 :(得分:0)
在Year
和group by
中添加partition by
可以解决问题
SELECT AÑO,
mn,
Count(DISTINCT( valuacion1 )) AS Val_Dist_Mes,
Sum(flag) AS Val_Dist_Mes_Nuevas,
Sum(Sum(flag))
OVER (
partition BY AÑO
ORDER BY mn rows UNBOUNDED PRECEDING) AS Val_Dist_Acum
FROM (SELECT valuacion1,
Extract(month FROM fecha_captura) AS mn,
Extract(year FROM fecha_captura) AS AÑO,
CASE -- find the first year when a customer placed an order
WHEN Extract(month FROM fecha_captura) = Min (
Extract(month FROM fecha_captura))
OVER (
partition BY valuacion1, Extract(year
FROM fecha_captura)) THEN 1
ELSE 0
END AS flag
FROM table2
GROUP BY fecha_captura,
valuacion1) AS dt
GROUP BY mn,
AÑO