Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
Postgres: Get which part is a record within a set

I am trying to obtain or calculate the information of each record within a set (installment and partial value), I explain below with an example:

| pay        |  date       | value |
|:-----------|------------:|:-----:|
| 910006603  | 2017-04-19  | 30    |
| 910006603  | 2017-04-21  | 30    |
| 910006603  | 2017-04-23  | 30    |
| 910006603  | 2017-04-25  | 30    |  
| 910006604  | 2017-04-14  | 45    | 
| 910006604  | 2017-04-18  | 45    |

With this information I must add two other columns, indicating the installment and the partial value of it, so that it results:

| pay        |  date       | value | insta | partial|
|:-----------|------------:|:-----:|:-----:|:------:|
| 910006603  | 2017-04-19  | 30    | 1     | 30     |
| 910006603  | 2017-04-21  | 30    | 2     | 60     |
| 910006603  | 2017-04-23  | 30    | 3     | 90     |
| 910006603  | 2017-04-25  | 30    | 4     | 120    |

| 910006604  | 2017-04-14  | 45    | 1     | 45     | 
| 910006604  | 2017-04-18  | 45    | 2     | 90     |

I hope to be explicit enough, thank you.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can get it by using ROW_NUMBER() and SUM() window functions.

create table payments(pay int, dt date, value int);
insert into payments values
(910006603, '2017-04-19', 30),
(910006603, '2017-04-21', 30),
(910006603, '2017-04-23', 30),
(910006603, '2017-04-25', 30),
(910006604, '2017-04-14', 45),
(910006604, '2017-04-18', 45);
select pay, 
       dt as date, 
       value, 
       row_number() over (partition by pay order by dt) as insta,
       sum(value) over (partition by pay order by dt) as partial
from   payments;
      pay | date       | value | insta | partial
--------: | :--------- | ----: | ----: | ------:
910006603 | 2017-04-19 |    30 |     1 |      30
910006603 | 2017-04-21 |    30 |     2 |      60
910006603 | 2017-04-23 |    30 |     3 |      90
910006603 | 2017-04-25 |    30 |     4 |     120
910006604 | 2017-04-14 |    45 |     1 |      45
910006604 | 2017-04-18 |    45 |     2 |      90

dbfiddle here

over 4 years ago · Santiago Trujillo Denunciar

0

Try this and let me know in case of any queries.

select pay, date, value,
       (select sum(value) from t t2 where t2.pay = t.pay and t2.date <= t.date) as partial,
       (select count(*) from t t3 where t3.pay = t.pay and t3.date <= t.date) as insta
from table t;
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda