使用row_number()函数时如何排序数据?

时间:2018-03-20 17:02:40

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

我想在使用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

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

3 个答案:

答案 0 :(得分:0)

也许帮助 试试这个

;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

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

只需使用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;