zoukankan      html  css  js  c++  java
  • 自查询一例:求连续出现的次数

     数据表如下:

    代码
     create table [tb]([value1] int,[value2] int
        
    insert [tb] 
        
    select 1,12 union all 
        
    select 1,13 union all 
        
    select 1,23 union all 
        
    select 0,14 union all 
        
    select 0,15 union all 
        
    select 1,16 union all 
        
    select 0,23 union all 
        
    select 0,22 union all 
        
    select 1,21 union all 
        
    select 1,12

    求value1列中的数字连续出现的次数:

    代码
    select * from tb

    select identity(int,1,1as id,* 
    into #db
    from tb

    select * from #db

    select a.value1,a.value2,nums=(
    select count(1from #db where value1=a.value1 
    and id<=a.id
    and id>=(select isnull(max(id),0from #db where id<a.id and value1 !=a.value1)
    )
    from #db as a

    drop table #db

    结果如下:

  • 相关阅读:
    静态库,动态库
    vim
    消息队列-Rabbitmq处理消息及在Spring中的应用
    消息队列 -- 队列(Queue)和主题(Topic)
    Sing的签名算法
    Jquery
    VUE
    node
    vue 加载静态图片
    vue :style 动态绑定style
  • 原文地址:https://www.cnblogs.com/Fskjb/p/1713248.html
Copyright © 2011-2022 走看看