<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.

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

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

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                      <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'>