• <tfoot id='ex0eC'></tfoot>

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

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

    1. <legend id='ex0eC'><style id='ex0eC'><dir id='ex0eC'><q id='ex0eC'></q></dir></style></legend>
        <bdo id='ex0eC'></bdo><ul id='ex0eC'></ul>

        如何在SQL中返回每个组的增量组号

        How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)

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

            <tfoot id='IUDvE'></tfoot>
              • <bdo id='IUDvE'></bdo><ul id='IUDvE'></ul>

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

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

                  本文介绍了如何在SQL中返回每个组的增量组号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在 SQL 中创建一个数据查询,以递增地编号行组,在公共日期时间分组,并在下一个日期时间保持组号"递增,依此类推.正如我在使用 partition by 语句时所看到的那样,这些组号"不得为每个组重置.这是我的示例数据:

                  I would like create a data query in SQL to incrementally number groups of rows, grouped on a common datetime and keep the "group numbers" incrementing on the next datetime and so on. These "group numbers" must not reset for each group as I have seen when using the partition by statement. Here is my sample data:

                  ts_DateTime          |ID   |Value|RowFilter|RequiredResult
                  --------------------------
                  2013/01/09 09:23:16  |8009 |0    |1        |1
                  2013/01/09 09:23:16  |8010 |0    |2        |1
                  2013/01/09 09:23:16  |8026 |0    |3        |1
                  
                  2013/01/09 09:23:22  |8026 |0    |1        |2
                  
                  2013/01/09 09:23:28  |8009 |0    |1        |3
                  2013/01/09 09:23:28  |8010 |0    |2        |3
                  2013/01/09 09:23:28  |8026 |0    |3        |3
                  
                  2013/01/09 09:27:03  |8009 |0    |1        |4
                  2013/01/09 09:27:03  |8010 |0    |2        |4
                  2013/01/09 09:27:03  |8026 |0    |3        |4
                  
                  2013/01/09 09:27:09  |8009 |0    |1        |5
                  2013/01/09 09:27:09  |8010 |0    |2        |5
                  2013/01/09 09:27:09  |8026 |0    |3        |5
                  
                  2013/01/09 09:27:15  |8009 |0    |1        |6
                  2013/01/09 09:27:15  |8010 |0    |2        |6
                  2013/01/09 09:27:15  |8026 |0    |3        |6
                  
                  
                  

                  我用来获得这些结果的查询是:

                  The query I am using to get these results is :

                  select hl.ts_DateTime,  hl.Tagname as [ID],  hl.TagValue as [Value],
                  ROW_NUMBER() OVER (PARTITION BY hl.ts_datetime ORDER BY hl.tagname) AS RowFilter
                  from Table1 hl
                  

                  所以基本上,查看 RowFilter 列,我得到每个 ts_DateTime 分区的唯一 ROW 编号.我真正需要的是,对于每个 ts_DateTime 分区,RowFilter 列应该看起来像所需的结果列.

                  So basically, looking at the RowFilter column, I am getting a unique ROW number per ts_DateTime partition. What I actually need is that for each ts_DateTime partition the RowFilter column should look like the Required result column.

                  推荐答案

                  你不应该使用 ROW_NUMBER(),

                  • 改用DENSE_RANK()
                  • 删除PARTITION BY

                  查询,

                  SELECT hl.ts_DateTime,  
                         hl.Tagname as [ID],  
                         hl.TagValue as [Value],
                         DENSE_RANK() OVER (ORDER BY ts_datetime) AS RowFilter
                  FROM   Table1 hl 
                  ORDER  BY RowFilter
                  

                  • SQLFiddle 演示
                  • 这篇关于如何在SQL中返回每个组的增量组号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 查询中包含缺失的月份)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                1. <legend id='d9eV7'><style id='d9eV7'><dir id='d9eV7'><q id='d9eV7'></q></dir></style></legend>
                    <bdo id='d9eV7'></bdo><ul id='d9eV7'></ul>
                    <tfoot id='d9eV7'></tfoot>

                            <tbody id='d9eV7'></tbody>

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

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