将行合并为一行 - oracle 10g

时间:2011-10-24 15:03:02

标签: sql oracle oracle10g

我有一些数据分布在两个表中。第二个表以行号存储数据。有没有办法可以在一行中显示行号而不是多行?见截图。 Line Numbers jumbled

SELECT DISTINCT II.FC,
            II.GN,
            II.PB,
            II.DTI,
            TL.LINENUMBER,
            TL.TEXTLINE
FROM    (   ABC.ITD ITD
       INNER JOIN
          ABC.TEXTLINE TEXTLINE
       ON (ITD.DTI = TEXTLINE.TEXTID))
   INNER JOIN
      ABC.II II
   ON (II.ITEMID = ITD.ITEMID)
WHERE (II.FC = 'J') AND (TEXTLINE.TEXTLINE IS NOT NULL)
ORDER BY ITD.DTI ASC

1 个答案:

答案 0 :(得分:1)

您需要创建一个函数来查找和连接文本行:

CREATE OR REPLACE FUNCTION FN_APPEND_TEXT(idText IN NUMBER) RETURN VARCHAR2
IS
   CURSOR crsText IS
      SELECT TL.LINENUMBER,
             TL.TEXTLINE
        FROM ABC.TEXTLINE TL
       WHERE TL.TEXTID = idText
         AND TL.TEXTLINE IS NOT NULL
       ORDER BY TL.LINENUMBER ASC;

   strReturn   VARCHAR2(4000);
BEGIN

   FOR recText IN crsText
   LOOP
      strReturn := strReturn || recText.TEXTLINE;
   END LOOP;

   RETURN strReturn;

END FN_APPEND_TEXT;

然后修改SQL以调用函数:

SELECT DISTINCT II.FC,
       II.GN,
       II.PB,
       II.DTI,
       FN_APPEND_TEXT(II.DTI) Instructions
  FROM ABC.ITD II
 WHERE II.FC = 'J'
 ORDER BY II.DTI ASC;