0

我有一个 table1,我想将其转换为预期的表格。

  • 列的预期表逻辑:
  • cal:来自table1的cal。ID 来自 table1 的 ID。
  • 代码:这取决于我们在 f_a 中是否有一个值,然后我们用 fp 作为代码创建一个新记录。与它相对应,我们检查是否填充了 f_a,如果是,则我们从 f_a 中获取该日期并放入同一 ID 的 Al 列中。我们还检查是否填充了 f_pl,如果是,则我们从中获取日期并将其放在 pl 列中。
  • 如果代码是 lp,那么我们检查是否填充了 l_a,然后我们取该日期并将该日期放在 Al 中,以获取该代码和 Id。此外,我们检查是否填充了 lpl,如果是,则我们取该日期并将其放入 pl。

我只是 SQL 的初学者,所以对于如何开始它对我来说有点不知所措。请发布一些解决方案。

表格1:

ID  f_a            l_a           f_pl       lpl               cal
CNT 6/20/2018      6/28/2018                6/28/2018         1/31/2020

预期输出:

ID  Cal             code          pl            Al 
CNT 1/31/2020       lp        6/28/2018   6/28/2018 
CNT 1/31/2020       fp                    6/20/2018 

更新:我在表中有更多的 ID,所以不是 CNT 是唯一的 ID。如果我使用 unpivot,那么它应该对所有 ID 遵循相同的逻辑。

4

3 回答 3

1

这是一个关于如何将列反转为行的问题。在 Oracle 中,我建议使用横向连接:

select t.id, t.cal, x.*
from mytable t
cross apply (
    select 'lp' as code, t.lpl as pl, l_a as al from dual
    union all
    select 'fp', t.f_pl, t.f_a from dual
) x

此语法在 Oracle 12.1 及更高版本中可用。在早期版本中,您将使用union all

select id, cal, 'lp' as code, lpl as pl, l_a as al from mytable
union all
select id, cal, 'lp' as code, 'fp', f_pl, f_a from mytable
于 2020-10-22T14:39:34.613 回答
1

您可以使用UNPIVOT多个列,然后对日期进行所需的检查:

with a as (
  select
    'CNT' as ID,
    date '2018-06-20' as f_a,
    date '2018-06-28' as l_a,
    cast(null as date) as f_pl,
    date '2018-06-28' as l_pl,
    date '2020-01-31' as cal
  from dual
)
select *
from a
unpivot(
  (pl, al) for code in ((l_pl, l_a) as 'lp', (f_pl, f_a) as 'fp')
) up
ID  | CAL       | CODE | PL        | AL
CNT | 31-JAN-07 | lp   | 28-JUN-18 | 28-JUN-18
CNT | 31-JAN-07 | fp   |           | 20-JUN-18

这里的工作示例

于 2020-10-22T15:02:15.580 回答
0

请尝试这个不依赖于版本的脚本:

-- Here we select columns foom source table. Please change the names if they are different
with r as (
  select
    ID,
    f_a,
    l_a,
    f_pl,
    lpl, -- Here not sure if example is wrong or no underscore in column deffinition
    cal
  from table_1 --  Please put real table name here
)
select * from (
select r.id, r.cal, 'lp' as code, r.l_pl as pl, l_a as ai
from r
where r.l_a is not null
union all
select r1.id, r1.cal, 'pl', r1.f_pl, r1.f_a 
from r r1
where r1.f_a is not null
)
order by id, cal, code;
于 2020-10-23T08:45:53.970 回答