国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

根據排序定義表排序數據

2024-07-21 02:05:38
字體:
來源:轉載
供稿:網友

/*--原帖地址:http://community.csdn.net/expert/topic/3866/3866872.xml?temp=.2154199--*/

--測試數據create table tb1(id int,col1 varchar(10),col2 int)insert tb1 select 1,'aa',111union  all select 2,'aa',111union  all select 3,'aa',111union  all select 4,'bb',222union  all select 5,'bb',222  union  all select 6,'cc',333  union  all select 7,'cc',333  union  all select 8,'cc',333union  all select 9,'cc',333 

create table tb2(col1 varchar(10),size int)insert tb2 select 'aa',2union  all select 'cc',2  --改改順序union  all select 'bb',0go

/*--處理要求:

tb1 與 tb2 通過 col2 關聯,tb2的col1包含所有tb1.col1

要求寫函數實現如下排序:tb1中的col1按照tb2中的col1定義的記錄數,進行排列。如:在tb2中:aa 的size=2,就先排2條aa記錄cc 的size=2, 并且在aa后面,所以接著排2條cc的記錄而bb 在tb2中定義是0條,則不參與排序,始終顯示在最后重復循環,不夠的由后面的記錄補上

最終實現如下排序結果:

id          col1       col2        ----------- ---------- ----------- 1           aa         1112           aa         1116           cc         3337           cc         3333           aa         1118           cc         3339           cc         3334           bb         2225           bb         222--*/

--排序函數create function f_sort(@id int,@col1 varchar(10))returns varchar(20)asbegin declare @i int,@size int,@j int set @i=0 select @i=case when @size is null then @i+1 else @i end  ,@size=case when @col1=col1 then size else @size end from tb2 if @size=0 return(replicate(20,'9')) select @j=count(*) from tb1 where id<@id and [email protected] return(right(10000000000+isnull(@j,0)/@size,10)+right([email protected],10))endgo

--調用實現查詢select * from tb1 aorder by dbo.f_sort(id,col1)go

--刪除測試drop table tb1,tb2drop function f_sort
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 江陵县| 万宁市| 肃北| 七台河市| 新闻| 钟祥市| 永春县| 铜川市| 巫山县| 柞水县| 南皮县| 兰溪市| 田阳县| 无锡市| 绥宁县| 衡阳县| 灌阳县| 巴林左旗| 津南区| 清新县| 翼城县| 宁津县| 大冶市| 佳木斯市| 荣昌县| 南安市| 巴彦县| 鞍山市| 高州市| 双柏县| 南丰县| 阜平县| 南宁市| 如皋市| 凤山县| 玛纳斯县| 青神县| 平乐县| 昌图县| 和平县| 湟中县|