zoukankan      html  css  js  c++  java
  • 统计相邻相同行数量 MSSQL Server / 疑难问题

    A
    A
    A
    B
    B
    A
    C
    C
    B
    结果:
    A 3
    B 2
    A 1
    C 2
    B 2

    解决方法:

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([col] varchar(1))
    insert [tb]
    select 'A' union all
    select 'A' union all
    select 'A' union all
    select 'B' union all
    select 'B' union all
    select 'A' union all
    select 'C' union all
    select 'C' union all
    select 'B'

    ;with fly as
    (select *,rn=row_number() over(order by getdate()) from tb)

    select col,count(1) as cnt
    from(
    select col,rn,
    gid=rn-(select count(1) from fly where col=t.col and rn<t.rn)
    from fly t
    ) a
    group by col,gid
    order by min(rn)
    /**
    col cnt
    ---- -----------
    A 3
    B 2
    A 1
    C 2
    B 1

    (5 行受影响)

    *
    */



  • 相关阅读:
    Python 字符串
    python 元组用法
    python 字典用法
    环境配置
    桥式整流以及电容作用
    三角序列的正交性
    MDS
    ISOMAP
    randperm
    数据库~Mysql里的Explain说明
  • 原文地址:https://www.cnblogs.com/bugY/p/2199333.html
Copyright © 2011-2022 走看看