背著手扇扇子的人
          往事隨風......前事如夢......
          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 閱讀(983) 評論(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)

          隨筆分類

          隨筆檔案

          文章檔案

          相冊

          收藏夾

          朋友

          搜索

          •  

          積分與排名

          • 積分 - 23286
          • 排名 - 1595

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乌拉特后旗| 宁武县| 永清县| 铜山县| 天台县| 贡嘎县| 兴义市| 逊克县| 尼玛县| 南雄市| 黄陵县| 扬中市| 克什克腾旗| 山阳县| 腾冲县| 佛坪县| 梅州市| 桦川县| 津南区| 马边| 吴川市| 无为县| 大化| 新津县| 扎赉特旗| 布尔津县| 湘阴县| 酉阳| 吉安县| 柏乡县| 湖北省| 白朗县| 武邑县| 福清市| 榕江县| 陇南市| 大连市| 盐山县| 洞头县| 杨浦区| 汨罗市|