<i id='Ot6ZD'><tr id='Ot6ZD'><dt id='Ot6ZD'><q id='Ot6ZD'><span id='Ot6ZD'><b id='Ot6ZD'><form id='Ot6ZD'><ins id='Ot6ZD'></ins><ul id='Ot6ZD'></ul><sub id='Ot6ZD'></sub></form><legend id='Ot6ZD'></legend><bdo id='Ot6ZD'><pre id='Ot6ZD'><center id='Ot6ZD'></center></pre></bdo></b><th id='Ot6ZD'></th></span></q></dt></tr></i><div id='Ot6ZD'><tfoot id='Ot6ZD'></tfoot><dl id='Ot6ZD'><fieldset id='Ot6ZD'></fieldset></dl></div>

  • <small id='Ot6ZD'></small><noframes id='Ot6ZD'>

    <legend id='Ot6ZD'><style id='Ot6ZD'><dir id='Ot6ZD'><q id='Ot6ZD'></q></dir></style></legend>
          <bdo id='Ot6ZD'></bdo><ul id='Ot6ZD'></ul>
        <tfoot id='Ot6ZD'></tfoot>

      1. 计数表中值的连续出现次数

        Count Number of Consecutive Occurrence of values in Table(计数表中值的连续出现次数)
      2. <legend id='MQFKG'><style id='MQFKG'><dir id='MQFKG'><q id='MQFKG'></q></dir></style></legend>
        <i id='MQFKG'><tr id='MQFKG'><dt id='MQFKG'><q id='MQFKG'><span id='MQFKG'><b id='MQFKG'><form id='MQFKG'><ins id='MQFKG'></ins><ul id='MQFKG'></ul><sub id='MQFKG'></sub></form><legend id='MQFKG'></legend><bdo id='MQFKG'><pre id='MQFKG'><center id='MQFKG'></center></pre></bdo></b><th id='MQFKG'></th></span></q></dt></tr></i><div id='MQFKG'><tfoot id='MQFKG'></tfoot><dl id='MQFKG'><fieldset id='MQFKG'></fieldset></dl></div>
          <tbody id='MQFKG'></tbody>
            <bdo id='MQFKG'></bdo><ul id='MQFKG'></ul>
              <tfoot id='MQFKG'></tfoot>

                  <small id='MQFKG'></small><noframes id='MQFKG'>

                  本文介绍了计数表中值的连续出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有下表

                  create table #t (Id int, Name char)
                  
                  insert into #t values
                  (1, 'A'),
                  (2, 'A'),
                  (3, 'B'),
                  (4, 'B'),
                  (5, 'B'),
                  (6, 'B'),
                  (7, 'C'),
                  (8, 'B'),
                  (9, 'B')
                  

                  我想计算名称列中的连续值

                  I want to count consecutive values in name column

                  +------+------------+
                  | Name | Repetition |
                  +------+------------+
                  | A    |          2 |
                  | B    |          4 |
                  | C    |          1 |
                  | B    |          2 |
                  +------+------------+
                  

                  我尝试过的最好的事情是:

                  The best thing I tried is:

                  select Name
                  , COUNT(*) over (partition by Name order by Id) AS Repetition
                  from #t
                  order by Id
                  

                  但它没有给我预期的结果

                  but it doesn't give me expected result

                  推荐答案

                  一种方法是行号不同:

                  select name, count(*) 
                  from (select t.*,
                               (row_number() over (order by id) -
                                row_number() over (partition by name order by id)
                               ) as grp
                        from t
                       ) t
                  group by grp, name;
                  

                  如果您运行子查询并分别查看每个行号的值,然后查看差异,则逻辑最容易理解.

                  The logic is easiest to understand if you run the subquery and look at the values of each row number separately and then look at the difference.

                  这篇关于计数表中值的连续出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                      <legend id='UUtM5'><style id='UUtM5'><dir id='UUtM5'><q id='UUtM5'></q></dir></style></legend>
                        <bdo id='UUtM5'></bdo><ul id='UUtM5'></ul>
                          <tbody id='UUtM5'></tbody>
                      • <tfoot id='UUtM5'></tfoot>

                        1. <i id='UUtM5'><tr id='UUtM5'><dt id='UUtM5'><q id='UUtM5'><span id='UUtM5'><b id='UUtM5'><form id='UUtM5'><ins id='UUtM5'></ins><ul id='UUtM5'></ul><sub id='UUtM5'></sub></form><legend id='UUtM5'></legend><bdo id='UUtM5'><pre id='UUtM5'><center id='UUtM5'></center></pre></bdo></b><th id='UUtM5'></th></span></q></dt></tr></i><div id='UUtM5'><tfoot id='UUtM5'></tfoot><dl id='UUtM5'><fieldset id='UUtM5'></fieldset></dl></div>

                          <small id='UUtM5'></small><noframes id='UUtM5'>