使用案例在select语句中获取子查询数据

时间:2019-10-03 16:53:35

标签: sql oracle

我有以下格式的数据

   ID  name   order    start Time                       end time
    2   null    1   30-SEP-19 11.13.39.000000000 PM  30-SEP-19 11.15.21.000000000 PM
    5   null    2   30-SEP-19 11.15.21.000000000 PM  30-SEP-19 11.19.25.000000000 PM
    2   null    4   30-SEP-19 11.20.08.000000000 PM  30-SEP-19 11.20.59.000000000 PM
    5   null    5   30-SEP-19 11.21.00.000000000 PM  30-SEP-19 11.25.02.000000000 PM

第一行是ID,第三行是订单

我的要求是,当控件位于第一行时,然后是第二列(名称),我需要编写一个单独的查询,在该查询中,我要从表中选择一条记录,该记录的数据少于第一行的结束时间,并且该记录应继续直到所有行都完成。

我尝试了所有方法,例如为第二列编写子查询,但是每次尝试均失败。

编辑

表2:

Name1   30-SEP-19 11.13.39.000000000 PM
name2   30-SEP-19 11.16.39.000000000 PM

如果您看到上面的表,我需要使用表1中的结束时间查询该表,因此最终结果将是

   ID  name   order    start Time                       end time
    2   name1   1   30-SEP-19 11.13.39.000000000 PM  30-SEP-19 11.15.21.000000000 PM //since name1 record end time is less than record 1 end time
    5   null    2   30-SEP-19 11.15.21.000000000 PM  30-SEP-19 11.19.25.000000000 PM
    2   null    4   30-SEP-19 11.20.08.000000000 PM  30-SEP-19 11.20.59.000000000 PM
    5   null    5   30-SEP-19 11.21.00.000000000 PM  30-SEP-19 11.25.02.000000000 PM

现在提交的第二条记录名称应该为end time >= record1 endtime and end time < record2 end time

编辑

表2:

ID  Name           Start Time                        End Time
2   780     30-SEP-19 11.13.39.000000000 PM 30-SEP-19 11.15.21.000000000 PM
5   782     30-SEP-19 11.15.21.000000000 PM 30-SEP-19 11.19.25.000000000 PM
2   786     30-SEP-19 11.20.08.000000000 PM 30-SEP-19 11.20.59.000000000 PM
5   788     30-SEP-19 11.21.00.000000000 PM 30-SEP-19 11.25.02.000000000 PM

名称顺序不正确,我能够看到错误顺序的正确记录数。

表2的第一条记录中的

Name column数据应添加到表1的第一条记录中,依此类推,但这与所提供的解决方案的顺序不正确。同样,两个table2记录的末尾可以与表1相同,但小于表1,但不能大于表1。

编辑

表1:

   ID  Type Order      start time                          end time
    2   2   1      30-SEP-19 11.13.39.000000000 PM  30-SEP-19 11.15.21.000000000 PM
    5   2   2      30-SEP-19 11.15.21.000000000 PM  30-SEP-19 11.19.25.000000000 PM
    2   2   4      30-SEP-19 11.20.08.000000000 PM  30-SEP-19 11.20.59.000000000 PM
    5   2   5      30-SEP-19 11.21.00.000000000 PM  30-SEP-19 11.25.02.000000000 PM

表2:

ID  Name     start time                               end time
2   780  30-SEP-19 11.13.39.000000000 PM    30-SEP-19 11.15.21.000000000 PM
5   782  30-SEP-19 11.15.21.000000000 PM    30-SEP-19 11.19.25.000000000 PM
2   786  30-SEP-19 11.20.08.000000000 PM    30-SEP-19 11.20.59.000000000 PM
5   788  30-SEP-19 11.21.00.000000000 PM    30-SEP-19 11.25.02.000000000 PM

现在最终结果将是

ID  Type Order    Name          start time                          end time
 2    2    1      780   30-SEP-19 11.13.39.000000000 PM 30-SEP-19 11.15.21.000000000 PM
 5    2    2      782   30-SEP-19 11.15.21.000000000 PM 30-SEP-19 11.19.25.000000000 PM
 2    2    4      786   30-SEP-19 11.20.08.000000000 PM 30-SEP-19 11.20.59.000000000 PM
 5    2    5      788   30-SEP-19 11.21.00.000000000 PM 30-SEP-19 11.25.02.000000000 PM

表2第一行中的名称列值应添加到表1中,直到最后一行。这里的结束时间是关键,并且我不能在结束时间上使用内部联接,因为对于表1和表2来说,内部联接可以相同,或者表2的结束时间可以小于表1但不能大于表1。

编辑

ID  Type Order    Name          start time                          end time
 2    2    4      780   30-SEP-19 11.20.08.000000000 PM 30-SEP-19 11.20.59.000000000 PM
 5    2    5      782   30-SEP-19 11.21.00.000000000 PM 30-SEP-19 11.25.02.000000000 PM

1 个答案:

答案 0 :(得分:1)

您可以在下面的查询中尝试-

SELECT T1.ID, T2.name, T1.order, T1.start_Time, MAX(T1.end_time)
FROM TABLE_1 T1
JOIN TABLE_2 T2 ON T1.end_time >= T2.end_time
                AND T1.ID = T2.ID
GROUP BY T1.ID, T2.name, T1.order, T1.start_Time
ORDER BY T1.order

或者,如果表中没有任何主键,并且可以肯定在两个表的end_time之前顺序始终是确定性的,则可以生成主键,然后基于该列进行联接-< / p>

SELECT T1.ID, T2.name, T1.order, T1.start_Time, MAX(T1.end_time)
FROM (select *, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY end_time) RN
      FROM TABLE_1)  T1
JOIN (select *, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY end_time)
      FROM TABLE_2) T2 ON T1.ID = T2.ID
                       AND T1.RN = T2.RN
GROUP BY T1.ID, T2.name, T1.order, T1.start_Time
ORDER BY T1.order