1

我有一张表格,格式如下:

| ID    | COURSE | PASS |
---------------------------
| 1     | 1      | 1      |
| 1     | 2      | 1      |    
| 1     | 3      | 1      |
| 1     | 4      | 0      |
| 1     | 5      | 0      |

我想要表格中的行:

| ID    | FAILED | PASSED |
---------------------------
| 1     | 4,5    | 1,2,3  |

我唯一想到的是这样的:

    select NVL(passed.id, failed.id), passed.test, failed.test from 
        (select id, listagg(course, ',') within group (order by course) test from table1 where pass = 1 group by id ) passed 
    full outer join
        (select id, listagg(course, ',') within group (order by course) test from table1 where pass = 0 group by id ) failed
    on passed.id = failed.id

有没有办法在一个查询中做到这一点?

4

1 回答 1

11

尝试

select  id, 
  listagg(case when pass = 1 then course end, ',') within group (order by course) passed,
  listagg(case when pass = 0 then course end, ',') within group (order by course) failed
from table1
group by id

这是一个 sqlfiddle 演示

于 2013-07-14T13:33:41.380 回答