日期:2014-05-17  浏览次数:20975 次

请帮我看看这个SQL怎样写
有2个表 Table1 ,Table2 .
Table1的数据为
ID COL
1 A
1 B
2 B
2 A
2 C
3 A
3 C
3 B
4 A
4 C 
Table2的数据为
ID COL
1 B
1 A
2 B
2 A
3 C
3 B
3 A
4 A
4 B
然后 将id相同的 但col不同的 选出来
那么结果是 
ID TABLE1.COL TABLE2.COL
2 A,B,C A,B //col条数不同 
4 A,C A,B //col值不同

那么这样SQL这样写 谢谢!

------解决方案--------------------
SQL code

select a.id, a.c1, b.c2
  from (select id, max(c1) c1
          from (select id,
                       wm_concat(col) over(partition by id order by col) c1
                  from (select id, col from t1 order by id, col))
         group by id) a,
       (select id, max(c2) c2
          from (select id,
                       wm_concat(col) over(partition by id order by col) c2
                  from (select id, col from t2 order by id, col))
         group by id) b
 where a.id = b.id
   and c1 <> c2;