背著手扇扇子的人
          往事隨風......前事如夢......
          posts - 35,  comments - 17,  trackbacks - 0

          有人問這樣的sql該怎么實現:
          表數據和結構
          ?? ?CODE?NAME????B01????S01????B02????S02
          ????1??????????張三???????數學????80??
          ????1??????????張三????????????????????????????語文????75
          ????2??????????王五???????數學????70??
          ????2??????????王五????
          ????3??????????李四???????數學????50??
          ????3??????????李四???????????????????????????語文????88

          希望查詢出如下結果:
          ?? ?CODE?SUM_STR(NAME)????B01????SUM_STR(S01)????B02????SUM_STR(S02)
          ????1????????????????張三????????????????????數學????????????????80?????????? 語文????????????75
          ????2????????????????王五????????????????????數學????????????????70??
          ????3????????????????李四????????????????????數學????????????????50?????????? 語文?????????????88
          這個問題可以采用自定義的聚集函數來實現:

          create ? or ? replace ?type?strcat_type? as ?object?(
          ????cat_string?
          varchar2 ( 4000 ),
          ????static?
          function ?ODCIAggregateInitialize(cs_ctx? In ?Out?strcat_type)? return ? number ,
          ????member?
          function ?ODCIAggregateIterate(self? In ?Out?strcat_type,value? in ? varchar2 )? return ?

          number ,
          ????member?
          function ?ODCIAggregateMerge(self? In ?Out?strcat_type,ctx2? In ?Out?strcat_type)?

          return ? number ,
          ????member?
          function ?ODCIAggregateTerminate(self? In ?Out?strcat_type,returnValue?Out?

          varchar2 ,flags? in ? number )? return ? number
          )
          /


          ------------------------------------

          create ? or ? replace ?type?body?strcat_type? is
          ??static?
          function ?ODCIAggregateInitialize(cs_ctx? IN ?OUT?strcat_type)? return ? number
          ??
          is
          ??
          begin
          ??????cs_ctx?:
          = ?strcat_type(? null ?);
          ??????
          return ?ODCIConst.Success;
          ??
          end ;

          ??member?
          function ?ODCIAggregateIterate(self? IN ?OUT?strcat_type,
          ???????????????????????????????????????value?
          IN ? varchar2 ?)
          ??
          return ? number
          ??
          is
          ??
          begin
          ??????
          if ?self.cat_string? is ? null ? then
          ?????????self.cat_string?:
          = ?value;
          ??????
          end ? if ;
          ??????
          return ?ODCIConst.Success;
          ??
          end ;

          ??member?
          function ?ODCIAggregateTerminate(self? IN ?Out?strcat_type,
          ?????????????????????????????????????????returnValue?OUT?
          varchar2 ,
          ?????????????????????????????????????????flags?
          IN ? number )
          ??
          return ? number
          ??
          is
          ??
          begin
          ??????returnValue?:
          = ?self.cat_string;
          ??????
          return ?ODCIConst.Success;
          ??
          end ;

          ??member?
          function ?ODCIAggregateMerge(self? IN ?OUT?strcat_type,
          ?????????????????????????????????????ctx2?
          IN ?Out?strcat_type)
          ??
          return ? number
          ??
          is
          ??
          begin
          ?????? if self.cat_string is null then
          ?????????????????? self.cat_string :=? ctx2.cat_string;
          ????????? end if;
          ?????? return ?ODCIConst.Success;
          ??
          end ;

          end ;
          /

          -------------------

          CREATE ? OR ? REPLACE ? FUNCTION ?sum_str(input? varchar2 ?)
          RETURN ? varchar2
          PARALLEL_ENABLE?AGGREGATE?USING?strcat_type;
          /

          -------最后查詢語句:

          select ?code,sum_str(name),?sum_str(b01)?b01,sum_str(s01)?,sum_str(b02)?b02,sum_str(s02)
          from ?javaeye? group ? by ?code? order ? by ?code
          posted on 2009-01-05 21:55 kebo 閱讀(979) 評論(4)  編輯  收藏 所屬分類: oracle

          FeedBack:
          # re: 一個sql寫法
          2009-01-06 15:19 | 徐堯
          mysql有函數,可以分組拼接字符串  回復  更多評論
            
          # re: 一個sql寫法
          2009-01-06 15:56 | ccc
          nc的表設計  回復  更多評論
            
          # re: 一個sql寫法
          2009-01-06 17:27 | kebo
          確實是比較nc的表設計,只是別人問道怎么辦,幫別人說了一下而已,主要其實想說的聚集函數的自定義寫法而已。呵呵,見笑  回復  更多評論
            
          # re: 一個sql寫法
          2009-01-06 17:28 | kebo
          還不是完全的分組拼接字符串,不過也差不多  回復  更多評論
            

          <2009年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          相冊

          收藏夾

          朋友

          搜索

          •  

          積分與排名

          • 積分 - 23213
          • 排名 - 1598

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 和田市| 吉首市| 黑山县| 新竹县| 措勤县| 东乡族自治县| 元朗区| 如皋市| 泰州市| 周宁县| 台山市| 庄浪县| 西峡县| 温州市| 大化| 新宁县| 宁化县| 青田县| 合阳县| 汝阳县| 甘谷县| 金山区| 陆川县| 灵石县| 武威市| 博湖县| 来安县| 得荣县| 疏附县| 温宿县| 杭锦旗| 三门县| 龙海市| 旌德县| 舒城县| 宝山区| 漳浦县| 青河县| 双流县| 建瓯市| 河东区|