sql loader将可变数量的物理记录与单个物理记录相关联

时间:2011-09-12 13:33:29

标签: sql oracle sql-loader

我有以下数据:

Aapple mango wood
Bpine tea orange
Bnuts blots match
Ajust another record

现在我希望每个以'A'开头的记录与以'B'开头的记录相关联,直到遇到另一个'A'记录或非'B'记录。 例如,根据以上数据, 我想检索以下数据(2条记录),

mango tea
mango blots

A记录后的B记录数量是可变的,即(一条记录可能跟随任意数量的B记录(下面数据中的3条记录)。

Aapple mango wood
Bpine tea orange
Bnuts blots match
Basdf asdf  asdf
Ajust another record

因此产生的输出将是

mango tea
mango blots
mango asdf

是否可以使用sql loader执行上述操作?任何帮助/指针都是最受欢迎的。

编辑: 我正在考虑使用CONTINUEIF子句,但似乎没有办法消除之前检索到的记录。 例如,如果我使用,

CONTINUEIF NEXT PRESERVE(1)='B'

我会一次性得到“芒果茶污渍asdf”而不是

"mango|tea"
"mango|blots"
"mango|asdf"

1 个答案:

答案 0 :(得分:1)

我想我会根据记录类型标识符将记录加载到2个单独的表中, 见:http://download.oracle.com/docs/cd/B19306_01/server.102/b14215/ldr_control_file.htm#i1005614

并使用recnum保存订单

请参阅:http://download.oracle.com/docs/cd/B10501_01/server.920/a96652/ch06.htm

然后你可以转换sql中的数据

SELECT 
    a.text,
    b.text,
    a.id,
    a.nxtid 
FROM 
(
    SELECT text,id, NVL(LEAD(seq,1) OVER (ORDER BY id),999999) AS NXTID
    FROM t1
) a
LEFT JOIN t2 B ON b.seq > a.id AND b.id < a.nxtid