简体   繁体   English

在SQL中将单行拆分为多行

[英]split single row into multiple rows in SQL

In my table there are two fields start and stop which stores the start time and stop time respectively. 在我的表格中,有两个字段start和stop分别存储开始时间和停止时间。

for example the Start time = 2014-01-01 23:43:00 and stop = 2014-01-03 03:33:00. 例如开始时间= 2014-01-01 23:43:00和停止= 2014-01-03 03:33:00。 This timestamp needs to brokendown to. 此时间戳需要细分。

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

as three different rows. 作为三个不同的行。

Here the problem is the difference in stop and start time varies say 1 day to 10 days. 这里的问题是停止和开始时间的差异,例如1天到10天。

To make it more complicate, in the above example i split the period on basis of date, this i need to split on basis of time ie. 为了使其更加复杂,在上面的示例中,我根据日期拆分了时间段,这需要根据时间拆分。 say split at time 02:30:00, so the spiting should e as follows. 假设在时间02:30:00分裂,所以吐痰应该如下。

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 02:30:00, as date 2014-01-03 
4=> 2014-01-03 02:30:01 - 2014-01-03 03:33:00, as date 2014-01-04 

Once the above split has done, i need to count the rows grouped by date. 完成上述拆分后,我需要计算按日期分组的行。

I'm using PostgreSQL . 我正在使用PostgreSQL

Can anyone throw some light on this !! 谁能对此有所启发!

Thanks! 谢谢!

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

Create a collateral table - period and insert all possible periods in the table for let's say from now() -10 years to now()+10 years . 创建一个抵押表period ,并将所有可能的期间插入表中,例如from now() -10 years to now()+10 years

In case of days periods it should be all days of the 20 years. 如果是天数,则应为20年中的所有天数。

After that you can select from the period table and JOIN your table with period extracting code 之后,您可以从period表中进行选择,并使用期间提取代码来联接表

You probably will need an additional table containing every date to join it to your base table. 您可能需要一个包含每个日期的附加表才能将其连接到基本表。

If you had a table like dates containing values like: 如果您有一个dates类的表,其中包含以下值:

date_col
2014-01-01 00:00:00
2014-01-02 00:00:00
2014-01-03 00:00:00
...

Then you can do a query like 然后您可以执行类似的查询

SELECT GREATEST(start_time, date_col), LEAST(end_time,date_col + interval 1 day) FROM base_table
JOIN dates ON date_col BETWEEN start_time AND end_time

SQL not tested but it shows tha idea. SQL未测试,但显示出想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM