<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. 计数表中值的连续出现次数

        时间:2023-10-10
      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.

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

                  上一篇:在 text 或 ntext 数据类型上替换 REPLACE 下一篇:树结构中的递归求和

                  相关文章

                    <legend id='UUtM5'><style id='UUtM5'><dir id='UUtM5'><q id='UUtM5'></q></dir></style></legend>
                      <bdo id='UUtM5'></bdo><ul id='UUtM5'></ul>
                  1. <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'>