如何在每组数据后的PL / SQL中添加换行符?

时间:2017-08-09 16:04:30

标签: oracle plsql

所以,让我说我有这样的事情:

select first_name, last_name, account_number
from accounts
having count(distinct account_number) > 2;

让我们产生以下结果:

John, Doe, 12345
Mary, Smith, 12345
Joe, Foo, 30000
Foo, Yung, 30000
Larry, Leisure, 30000

我想要做的是在每组分组数据之间设置换行符,以区分每个组并使其更具可读性。在这种情况下,我想在“12345”和“30000”帐号之间换行。有关如何做到这一点的任何想法?我玩过chr,但似乎无法正常工作。

P.S。目前使用SQL * Developer。

谢谢!

1 个答案:

答案 0 :(得分:0)

WITH
    tablea
    AS
        (SELECT 'John' FIRST, 'Doe' LAST, 12345 account_number
           FROM DUAL
         UNION ALL
         SELECT 'Mary', 'Smith', 12345
           FROM DUAL
         UNION ALL
         SELECT 'Joe', 'Foo', 30000
           FROM DUAL
         UNION ALL
         SELECT 'Foo', 'Yung', 30000
           FROM DUAL
         UNION ALL
         SELECT 'Larry', 'Leisure', 30000
           FROM DUAL)
SELECT account_number
     , FIRST
     ,    LAST
       || CASE
              WHEN lead (account_number)
                       OVER (
                           PARTITION BY account_number ORDER BY last
                       )
                       IS NULL
              THEN
                  CHR (13) || CHR (10)
              ELSE
                  NULL
          END as last

  FROM tablea
  order by account_number, last