Skip to main content
título editado
Enlace
Patricio Moracho
  • 61.1k
  • 12
  • 42
  • 72

Cómo ¿Cómo pedir de una tabla un elemento de cada posicionposición ordenado por timestamp en postgresql?

se añadieron 102 caracteres en el cuerpo
Origen Enlace

en este ejemplo:

select pos, times, companies, step from history_status order by pos DESC;
 pos |           times            | companies | step
-----+----------------------------+-----------+-----
 673 | 2022-08-24 03:21:33.429016 |       292 | 3220
 673 | 2022-08-24 03:23:03.624892 |       292 | 3222
 673 | 2022-08-24 03:21:10.593225 |       292 | 3219
 672 | 2022-08-19 01:19:37.608525 |       292 | 3219
 672 | 2022-08-19 01:38:20.096511 |       292 | 3222
 672 | 2022-08-19 01:19:49.375686 |       292 | 3220
 671 | 2022-08-19 01:19:37.608525 |       292 | 3219
 671 | 2022-08-19 01:19:49.350515 |       292 | 3220
 671 | 2022-08-19 01:38:20.096511 |       292 | 3222
 670 | 2022-08-17 10:55:49.724941 |       310 | 3476
 670 | 2022-08-17 10:55:34.02915  |       310 | 3475
 669 | 2022-08-17 10:51:30.20212  |       310 | 3476
 669 | 2022-08-17 10:52:29.800695 |       310 | 3478
 669 | 2022-08-17 10:51:05.01008  |       310 | 3475
 668 | 2022-08-11 23:39:48.759836 |       293 | 3234
 668 | 2022-08-11 23:42:36.903544 |       293 | 3234
 667 | 2022-08-11 23:19:55.063745 |       293 | 3234
 666 | 2022-08-11 23:14:38.335605 |       293 | 3234
 665 | 2022-08-11 23:14:38.335605 |       293 | 3234
 664 | 2022-08-11 23:02:48.66474  |       293 | 3236
 664 | 2022-08-11 23:03:43.27225  |       293 | 3238
 664 | 2022-08-11 23:01:46.27165  |       293 | 3235
 pos |           times            | companies | step
-----+----------------------------+-----------+-----
 673 | 2022-08-24 03:21:33.429016 |       292 | 3220
 673 | 2022-08-24 03:23:03.624892 |       292 | 3222
 673 | 2022-08-24 03:21:10.593225 |       292 | 3219
 672 | 2022-08-19 01:19:37.608525 |       292 | 3219
 672 | 2022-08-19 01:38:20.096511 |       292 | 3222
 672 | 2022-08-19 01:19:49.375686 |       292 | 3220
 671 | 2022-08-19 01:19:37.608525 |       292 | 3219
 671 | 2022-08-19 01:19:49.350515 |       292 | 3220
 671 | 2022-08-19 01:38:20.096511 |       292 | 3222
 670 | 2022-08-17 10:55:49.724941 |       310 | 3476
 670 | 2022-08-17 10:55:34.02915  |       310 | 3475
 669 | 2022-08-17 10:51:30.20212  |       310 | 3476
 669 | 2022-08-17 10:52:29.800695 |       310 | 3478
 669 | 2022-08-17 10:51:05.01008  |       310 | 3475
 668 | 2022-08-11 23:39:48.759836 |       293 | 3234
 668 | 2022-08-11 23:42:36.903544 |       293 | 3234
 667 | 2022-08-11 23:19:55.063745 |       293 | 3234
 666 | 2022-08-11 23:14:38.335605 |       293 | 3234
 665 | 2022-08-11 23:14:38.335605 |       293 | 3234
 664 | 2022-08-11 23:02:48.66474  |       293 | 3236
 664 | 2022-08-11 23:03:43.27225  |       293 | 3238
 664 | 2022-08-11 23:01:46.27165  |       293 | 3235

en este ejemplo:

select pos, times, companies, step from history_status order by pos DESC;
 pos |           times            | companies | step
-----+----------------------------+-----------+-----
 673 | 2022-08-24 03:21:33.429016 |       292 | 3220
 673 | 2022-08-24 03:23:03.624892 |       292 | 3222
 673 | 2022-08-24 03:21:10.593225 |       292 | 3219
 672 | 2022-08-19 01:19:37.608525 |       292 | 3219
 672 | 2022-08-19 01:38:20.096511 |       292 | 3222
 672 | 2022-08-19 01:19:49.375686 |       292 | 3220
 671 | 2022-08-19 01:19:37.608525 |       292 | 3219
 671 | 2022-08-19 01:19:49.350515 |       292 | 3220
 671 | 2022-08-19 01:38:20.096511 |       292 | 3222
 670 | 2022-08-17 10:55:49.724941 |       310 | 3476
 670 | 2022-08-17 10:55:34.02915  |       310 | 3475
 669 | 2022-08-17 10:51:30.20212  |       310 | 3476
 669 | 2022-08-17 10:52:29.800695 |       310 | 3478
 669 | 2022-08-17 10:51:05.01008  |       310 | 3475
 668 | 2022-08-11 23:39:48.759836 |       293 | 3234
 668 | 2022-08-11 23:42:36.903544 |       293 | 3234
 667 | 2022-08-11 23:19:55.063745 |       293 | 3234
 666 | 2022-08-11 23:14:38.335605 |       293 | 3234
 665 | 2022-08-11 23:14:38.335605 |       293 | 3234
 664 | 2022-08-11 23:02:48.66474  |       293 | 3236
 664 | 2022-08-11 23:03:43.27225  |       293 | 3238
 664 | 2022-08-11 23:01:46.27165  |       293 | 3235
Origen Enlace

Cómo pedir de una tabla un elemento de cada posicion ordenado por timestamp en postgresql?

Cual sería la forma correcta de solicitar en postgresql todos los datos por cada posición de esta tabla (básicamente necesito el ultimo "step" de cada posición (pos)) ??

 pos |           times            | companies | step
-----+----------------------------+-----------+-----
 673 | 2022-08-24 03:21:33.429016 |       292 | 3220
 673 | 2022-08-24 03:23:03.624892 |       292 | 3222
 673 | 2022-08-24 03:21:10.593225 |       292 | 3219
 672 | 2022-08-19 01:19:37.608525 |       292 | 3219
 672 | 2022-08-19 01:38:20.096511 |       292 | 3222
 672 | 2022-08-19 01:19:49.375686 |       292 | 3220
 671 | 2022-08-19 01:19:37.608525 |       292 | 3219
 671 | 2022-08-19 01:19:49.350515 |       292 | 3220
 671 | 2022-08-19 01:38:20.096511 |       292 | 3222
 670 | 2022-08-17 10:55:49.724941 |       310 | 3476
 670 | 2022-08-17 10:55:34.02915  |       310 | 3475
 669 | 2022-08-17 10:51:30.20212  |       310 | 3476
 669 | 2022-08-17 10:52:29.800695 |       310 | 3478
 669 | 2022-08-17 10:51:05.01008  |       310 | 3475
 668 | 2022-08-11 23:39:48.759836 |       293 | 3234
 668 | 2022-08-11 23:42:36.903544 |       293 | 3234
 667 | 2022-08-11 23:19:55.063745 |       293 | 3234
 666 | 2022-08-11 23:14:38.335605 |       293 | 3234
 665 | 2022-08-11 23:14:38.335605 |       293 | 3234
 664 | 2022-08-11 23:02:48.66474  |       293 | 3236
 664 | 2022-08-11 23:03:43.27225  |       293 | 3238
 664 | 2022-08-11 23:01:46.27165  |       293 | 3235