【问题标题】:how to return multiple records from single record in OracleOracle如何从一条记录返回多条记录
【发布时间】:2021-12-23 10:48:19
【问题描述】:

我有源 Tbl 之类的

CID   No_Of_Seats_Booked  Seat_Numbers
-------------------------------------
1            3              01A01B01C

Tgt 表 O/P

CID Seat_id
------------
1    01A    
1    01B    
1    01C

【问题讨论】:

  • 尝试以一种不在一列中保存多个数据的方式构建数据库。在这里,您可以创建另一个表“booking_seat”,其中包含 Booking_id、seat_id 列和一个包含所有可用座位 seat_id 和 seat_number 的表座位。这样,保存号码就过时了,您可以更改座位名称,所有旧预订仍然有效,而且您不会混淆名称。 “规范化”是这里的关键角色。

标签: sql oracle


【解决方案1】:

这是一种选择:

SQL> with test (cid, no_of_seats_booked, seat_numbers) as
  2    -- sample data
  3    (select 1, 3, '01A01B01C' from dual union all
  4     select 2, 2, '02A02B'    from dual)
  5  -- query you need begins here
  6  select cid,
  7         substr(seat_numbers, 1 + (column_value - 1) * 3, 3) seat_id
  8  from test cross join
  9    table(cast(multiset(select level from dual
 10                        connect by level <= no_of_seats_booked
 11                       ) as sys.odcinumberlist))
 12  order by cid, seat_id;

       CID SEA
---------- ---
         1 01A
         1 01B
         1 01C
         2 02A
         2 02B

SQL>

【讨论】:

    【解决方案2】:

    另一种方法,只是为了多样化:

    with demo(cid, no_of_seats_booked, seat_numbers) as
         ( select 1, 3, '01A01B01C' from dual union all
           select 2, 2, '02A02B' from dual
         )
    select d.cid
         , regexp_substr(d.seat_numbers, '...', 1, r.rnum) seat_id
    from   demo d
           cross apply (select rownum as rnum from dual connect by rownum <= d.no_of_seats_booked) r
    order  by d.cid, seat_id;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 2017-10-14
      • 1970-01-01
      相关资源
      最近更新 更多