• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 根據排序定義表排序數據

    發表于:2007-05-25來源:作者:點擊數: 標簽:原帖根據地址定義排序
    /*--原帖地址: 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',111 union all select 2,'aa',111 union all select 3,'aa',111 uni

    /*--原帖地址:
    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',111
    union  all select 2,'aa',111
    union  all select 3,'aa',111
    union  all select 4,'bb',222
    union  all select 5,'bb',222 
    union  all select 6,'cc',333 
    union  all select 7,'cc',333 
    union  all select 8,'cc',333
    union  all select 9,'cc',333 

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

    /*--處理要求:

    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         111
    2           aa         111
    6           cc         333
    7           cc         333
    3           aa         111
    8           cc         333
    9           cc         333
    4           bb         222
    5           bb         222
    --*/

    --排序函數
    create function f_sort(@id int,@col1 varchar(10))
    returns varchar(20)
    as
    begin
     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 col1=@col1
     return(right(10000000000+isnull(@j,0)/@size,10)+right(10000000000+@i,10))
    end
    go

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

    --刪除測試
    drop table tb1,tb2
    drop function f_sort


    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>