有一個帶有時間戳列的表:
create table histodureeconnexion ( id serial primary key, iduser int, connexion timestamp, deconnexion timestamp, duree_seconde integer );
我想根據連接列的日期部分獲取所有行和 duree_seconde 列的總和;這意味著我想對每一天的所有 duree_seconde 列值求和,例如,如果有這樣的資料:

如何獲得按連接列的日期部分分組的每個 duree_seconde 列的總和?那就是:我想得到這樣的東西:

uj5u.com熱心網友回復:
嘗試使用視窗函式并在PARTITION BY子句中將timestampto強制轉換為date:
SELECT *, SUM(duree_seconde) OVER (PARTITION BY connexion::date)
FROM histodureeconnexion;
演示: db<>fiddle
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/331691.html
標籤:sql PostgreSQL
