• <legend id='3fcqZ'><style id='3fcqZ'><dir id='3fcqZ'><q id='3fcqZ'></q></dir></style></legend>
    • <bdo id='3fcqZ'></bdo><ul id='3fcqZ'></ul>
        <tfoot id='3fcqZ'></tfoot>

        <small id='3fcqZ'></small><noframes id='3fcqZ'>

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

        sql仅按顺序分组

        sql group by only rows which are in sequence(sql仅按顺序分组)

              <tbody id='K5pzR'></tbody>
          • <tfoot id='K5pzR'></tfoot>
            • <bdo id='K5pzR'></bdo><ul id='K5pzR'></ul>

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

                <legend id='K5pzR'><style id='K5pzR'><dir id='K5pzR'><q id='K5pzR'></q></dir></style></legend>

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

                2. 本文介绍了sql仅按顺序分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有下表:

                  MyTable
                  ---------
                  | 1 | A |
                  | 2 | A |
                  | 3 | A |
                  | 4 | B |
                  | 5 | B |
                  | 6 | B |
                  | 7 | A |
                  | 8 | A |
                  ---------
                  

                  我需要 sql 查询输出以下内容:

                  I need the sql query to output the following:

                  ---------
                  | 3 | A |
                  | 3 | B |
                  | 2 | A |
                  ---------
                  

                  基本上我正在做一个 group by 但只针对在序列中在一起的行.有什么想法吗?

                  Basically I'm doing a group by but only for rows which are together in the sequence. Any ideas?

                  请注意,数据库位于 sql server 2008 上.有一篇关于此主题的帖子,但它使用了 oracle 的 lag() 函数.

                  Note that the database is on sql server 2008. There is a post on this topic however it uses oracle's lag() function.

                  推荐答案

                  这被称为孤岛"问题.使用 Itzik Ben Gan 的方法:

                  This is known as the "islands" problem. Using Itzik Ben Gan's approach:

                  ;WITH YourTable AS
                  (
                  SELECT 1 AS N, 'A' AS C UNION ALL
                  SELECT 2 AS N, 'A' AS C UNION ALL
                  SELECT 3 AS N, 'A' AS C UNION ALL
                  SELECT 4 AS N, 'B' AS C UNION ALL
                  SELECT 5 AS N, 'B' AS C UNION ALL
                  SELECT 6 AS N, 'B' AS C UNION ALL
                  SELECT 7 AS N, 'A' AS C UNION ALL
                  SELECT 8 AS N, 'A' AS C
                  ),
                       T
                       AS (SELECT N,
                                  C,
                                  DENSE_RANK() OVER (ORDER BY N) - 
                                  DENSE_RANK() OVER (PARTITION BY C ORDER BY N) AS Grp
                           FROM   YourTable)
                  SELECT COUNT(*),
                         C
                  FROM   T
                  GROUP  BY C,
                            Grp 
                  ORDER BY MIN(N)
                  

                  这篇关于sql仅按顺序分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 查询中包含缺失的月份)
                  <tfoot id='XISqG'></tfoot><legend id='XISqG'><style id='XISqG'><dir id='XISqG'><q id='XISqG'></q></dir></style></legend>

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

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

                            <bdo id='XISqG'></bdo><ul id='XISqG'></ul>