zoukankan      html  css  js  c++  java
  • SQL循环索引

    declare @tbl table(order_id varchar(20),dates datetime)
    insert @tbl(order_id,dates)
    select 'A','2014-1-1' union
    select 'A','2014-2-1' union
    select 'A','2014-2-2' union
    select 'B','2014-1-1' union
    select 'C','2014-1-1' union
    select 'D','2014-1-1' union
    select 'D','2014-1-2' union
    select 'E','2014-12-1' union
    select 'E','2014-12-2' union
    select 'E','2014-12-3' 
    
    declare @index int,@count int,@row int,@item_order varchar(20),@current_order varchar(20)
    
    select identity(int,1,1) as id,order_id,dates,cast(0 as int) as row_index into #tbl from @tbl order by order_id,dates
    select @index=1,@row=1,@count=@@rowcount
    
    while @row<=@count
    begin
        --首次循环
        if isnull(@item_order,'')=''
            select     @item_order=order_id from #tbl where id=@row
        --已经有做循环
        else
        begin
            select     @current_order=order_id from #tbl where id=@row
            if @current_order<>@item_order
            begin
                set @index=1
                set @item_order=@current_order
            end
        end
        --更新序列号
        update #tbl set row_index=@index where id=@row
        set @index=@index+1
        set @row=@row+1
    end
    select * from #tbl
    drop table #tbl
  • 相关阅读:
    MFC的自定义消息(用户消息)的定义与使用
    队列的使用
    接私活平台
    下载图标网站
    C#对象序列化
    C#中控件对比(DataGridView,ListView,TreeView,ComboBox)
    C#反射
    C#可选实参和命名实参
    C#扩展方法
    Lambda表达式
  • 原文地址:https://www.cnblogs.com/myjacky/p/4206250.html
Copyright © 2011-2022 走看看