【问题标题】:How sort data when use row_number() function?使用 row_number() 函数时如何对数据进行排序?
【发布时间】:2018-03-20 17:02:39
【问题描述】:

我想在使用row_number() 函数或其他方式时对数据进行排序。 当“e_type 列”更改最后状态时(当 s_type e_type 时),我对新的“sort_type 列”进行排序。

原始数据:

req_no   | seq   | s_date     | e_date     | s_type    | e_type   |
---------+-------+------------+------------+-----------+----------+
001      | 1     | 2017-01-01 | 2017-01-02 |  01       | 01       |
001      | 2     | 2017-01-02 | 2017-01-02 |  01       | 02       |
001      | 3     | 2017-01-02 | 2017-01-02 |  02       | 02       |
001      | 4     | 2017-01-02 | 2017-01-02 |  02       | 01       |
001      | 5     | 2017-01-02 | 2017-01-02 |  01       | 01       |
001      | 6     | 2017-01-02 | 2017-01-02 |  01       | 01       |
001      | 14    | 2017-01-03 | 2017-01-03 |  04       | 03       |
001      | 15    | 2017-01-03 | 2017-01-03 |  03       | 03       |
001      | 16    | 2017-01-03 | 2017-01-03 |  03       | 03       |
001      | 17    | 2017-01-03 | 2017-01-03 |  03       | 03       |

我现在从我的查询中得到这个结果:

req_no | seq  | s_date     | e_date     | s_type | e_type | sort_type
-------+------+------------+------------+--------+--------+----------
001    | 1    | 2017-01-01 | 2017-01-02 |  01    | 01     | 1
001    | 2    | 2017-01-02 | 2017-01-02 |  01    | 02     | 1

001    | 3    | 2017-01-02 | 2017-01-02 |  02    | 02     | 2
001    | 4    | 2017-01-02 | 2017-01-02 |  02    | 01     | 2

001    | 5    | 2017-01-02 | 2017-01-02 |  01    | 01     | 3 
001    | 6    | 2017-01-02 | 2017-01-02 |  01    | 01     | 4 
001    | 14   | 2017-01-03 | 2017-01-03 |  04    | 03     | 1
001    | 15   | 2017-01-03 | 2017-01-03 |  03    | 03     | 2
001    | 16   | 2017-01-03 | 2017-01-03 |  03    | 03     | 3
001    | 17   | 2017-01-03 | 2017-01-03 |  03    | 03     | 4

但我希望结果是这样的:

req_no | seq  | s_date     | e_date     | s_type | e_type | sort_type
-------+------+------------+------------+--------+--------+----------
001    | 1    | 2017-01-01 | 2017-01-02 |  01    | 01     | 1
001    | 2    | 2017-01-02 | 2017-01-02 |  01    | 02     | 1

001    | 3    | 2017-01-02 | 2017-01-02 |  02    | 02     | 2
001    | 4    | 2017-01-02 | 2017-01-02 |  02    | 01     | 2

001    | 5    | 2017-01-02 | 2017-01-02 |  01    | 01     | 3 (or not show)
001    | 6    | 2017-01-02 | 2017-01-02 |  01    | 01     | 4 (or not show)
001    | 14   | 2017-01-03 | 2017-01-03 |  04    | 03     | 5 (or 3)
001    | 15   | 2017-01-03 | 2017-01-03 |  03    | 03     | 6 (or not show)
001    | 16   | 2017-01-03 | 2017-01-03 |  03    | 03     | 7 (or not show)
001    | 17   | 2017-01-03 | 2017-01-03 |  03    | 03     | 8 (or not show)

002    | 1    | 2017-01-05 | 2017-01-05 |  01    | 02     | 1
002    | 2    | 2017-01-05 | 2017-01-05 |  03    | 03     | 2
002    | 3    | 2017-01-05 | 2017-01-05 |  03    | 03     | 2
002    | 4    | 2017-01-05 | 2017-01-05 |  03    | 04     | 2
002    | 5    | 2017-01-05 | 2017-01-05 |  04    | 04     | 3 (or not show)

这是我的 SQL Server 查询:

SELECT
    a.*, 
    ROW_NUMBER() OVER (PARTITION BY e_type ORDER BY req_no, seq) AS sort_type
FROM
    tb_listtype a

请帮助我。在此先感谢 ;)

【问题讨论】:

  • 我不明白你想要什么,应用了哪个逻辑来计算那些 (或不显示) 值?
  • 还有,req_no = 002 记录是从哪里来的?原始原始数据中没有它们

标签: sql sql-server function sql-server-2012 row-number


【解决方案1】:

也许可以帮助 试试这个

;With mytmpTable as
(
SELECT
    a.*, 
    ROW_NUMBER() OVER (PARTITION BY e_type ORDER BY req_no, seq) AS sort_type
FROM
    tb_listtype a
)
select * from mytmpTable where e_type=3 and sort_type>3

【讨论】:

    【解决方案2】:

    试试这个:

    select req_no, seq, s_date, e_date, s_type, e_type,
           sort_type - sort_type_temp [sort_type]
    from (
        select req_no, seq, s_date, e_date, s_type, e_type,
               sum(sort_type) over (partition by req_no order by s_date rows between unbounded preceding and current row) sort_type,
               case when s_type <> e_type then -1 else 0 end sort_type_temp
        from (
            select req_no, seq, s_date, e_date, s_type, e_type,
                   case when s_type <> e_type then 1 else 0 end sort_type
            from MY_TABLE
        ) a
    ) a
    

    【讨论】:

      【解决方案3】:

      只需使用order by:

      SELECT a.*, 
             ROW_NUMBER() OVER (PARTITION BY e_type ORDER BY req_no, seq) AS sort_type
      FROM tb_listtype a
      ORDER BY req_no, sort_type;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-13
        • 2019-07-08
        • 2019-06-17
        • 1970-01-01
        • 2021-10-23
        • 2012-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多