且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在SQL中将单行拆分为多行

更新时间:2023-02-06 08:19:21

我认为您的按时间分割期望的输出示例是错误的,应该以
为准

I think your "split by time" desired output sample is wrong and should in instead be this

1=> 2014-01-01 23:43:00 - 2014-01-02 02:30:00, as date 2014-01-01 
2=> 2014-01-02 02:30:01 - 2014-01-03 02:30:00, as date 2014-01-02 
3=> 2014-01-03 02:30:01 - 2014-01-03 03:33:00, as date 2014-01-03 

如果是这种情况,那就这样做

If that is the case then this do it

select day, count(*)
from (
    select generate_series(
        (start_time - interval '2 hours 30 minutes')::date,
        stop_time,
        interval '1 day'
    )::date as day
    from t
) s
group by day
order by day