0
select c.id, c.fname, c.lname, d.phoneList
  from customers c
  left outer join
       (
        select listagg(telNo, ',') within group (order by telNo) as phoneList
          from customerPhones 
         group by p.telNo
       ) d 
   on (c.id = d.id)

在上述查询中,我在语句中收到以下错误c.id = d.id

ORA-00904 "d.id" invalid identifier. 

它仅在我将其包含在 select 语句中并且需要将其包含在 group by 语句中的含义中才有效。有什么方法可以使用 d.id 而不必将其包含在 group by 语句中?

4

1 回答 1

1

只是简化到这个?

SQL> select * from customers;

        ID FNAME                LNAME
---------- -------------------- --------------------
         1 John                 Smith
         2 Joe                  Bloggs

SQL> select * from customerphones;

        ID TELNO
---------- --------------------
         1 0123456789
         1 0207983498
         2 0124339848
         2 09348374834
         2 02387694364

SQL> select c.id, c.fname, c.lname, listagg(telNo, ',') within group (order by telNo) as phoneList
  2    from customers c left outer join customerphones p on p.id = c.id
  3   group by c.id, c.fname, c.lname
  4  /

        ID FNAME                LNAME                PHONELIST
---------- -------------------- -------------------- ------------------------------
         1 John                 Smith                0123456789,0207983498
         2 Joe                  Bloggs               0124339848,02387694364,0934837
                                                     4834
于 2012-11-27T10:54:12.757 回答