修复postgresql中的持续时间值分钟:秒

dhxwm5r4  于 2023-02-08  发布在  PostgreSQL
关注(0)|答案(2)|浏览(181)

我有一个篮球队的boxscore数据集,mm:ss播放(例如44:46),当我将csv上传到表中时,大于24的值不知何故变为hh:mm(例如44:46变为44:46:00),而其他值保持不变(例如5:04),当前保存为varchar数据类型
我如何修正它以便我可以使用这些值来获得例如在队中具有最高上场时间的球员或每个球员的平均上场时间

wgmfuz8q

wgmfuz8q1#

/*input*/
create table tab1( interval varchar(100));
insert into tab1 values( '44:46:00' );
insert into tab1 values( '5:04' );
/*Query*/  
select case when charindex(':',interval,4) = 0 then interval  
else left(interval,charindex(':',interval,4)-1) end from tab1
brccelvz

brccelvz2#

WITH formatted_time AS (
    SELECT 
        player, 
        CAST(SUBSTRING(time_played, 1, 2) AS INT) * 60 + CAST(SUBSTRING(time_played, 4, 2) AS INT) AS time_in_seconds
    FROM 
        your_table
)
SELECT 
    player, 
    time_in_seconds, 
    time_in_seconds / 60 || ':' || time_in_seconds % 60 AS time_played_formatted
FROM 
    formatted_time

相关问题