隨筆-7  評論-24  文章-102  trackbacks-0

          內容引自:
          多行一列數據合并成一行一列數據
          http://topic.csdn.net/u/20090714/17/5FE6A0F7-CE78-4936-BE31-21D462236059.html

          在MySQL和Oracle中實現行合并
          http://www.aygfsteel.com/rain1102/archive/2009/06/24/283867.html


          SQL Server
          --SQL2005中的方法2 
          create table tb(id int, value varchar(10)) 
          insert into tb values(1'aa'
          insert into tb values(1'bb'
          insert into tb values(2'aaa'
          insert into tb values(2'bbb'
          insert into tb values(2'ccc'
          go 

          select id, [values]=stuff((select ','+[value] from tb t where id=tb.id 
          for xml path('')), 11''
          from tb 
          group by id 

          /* 
          id          values 
          ----------- -------------------- 
          1          aa,bb 
          2          aaa,bbb,ccc 

          (2 row(s) affected) 

          */ 

          drop table tb 


          MySQL
          select name , group_concat(email order by email separator ", ") as email from student group by name


          Oracle
          如果以上效果想在Oracle中顯示, 則比較復雜點了, 因為Oracle中沒有行合并函數, 則需要使用sys_connect_by_path()來實現, 代碼如下:
          select name, ltrim(sys_connect_by_path(email,','),',') email from(
          select name,email,
          row_number() 
          over(partition by name order by email) rn,
          count(*over(partition by name) cnt
          from student 
          where level = cnt
          start 
          with rn = 1
          connect 
          by prior name = name and prior rn + 1 = rn 
          posted on 2010-04-09 16:24 黃小二 閱讀(1710) 評論(0)  編輯  收藏 所屬分類: [DB].Oracle[DB].MySQL[DB].SQL Server
          主站蜘蛛池模板: 太仆寺旗| 鄱阳县| 精河县| 崇仁县| 竹溪县| 阿鲁科尔沁旗| 崇明县| 易门县| 大同县| 中山市| 于田县| 杭州市| 电白县| 明星| 新邵县| 旅游| 四子王旗| 平和县| 道孚县| 大关县| 凤山市| 依兰县| 黑河市| 凉城县| 玉田县| 宜良县| 雷山县| 藁城市| 高尔夫| 台东市| 雅江县| 阜南县| 抚顺县| 视频| 米脂县| 洱源县| 常熟市| 巴塘县| 房产| 始兴县| 基隆市|