如何处理 SQL 服务器中的序列?

How to deal with sequence in SQL Server?

我在 SQL 服务器中有一个问题处理顺序。

我要得到result01。

这是示例 table。

CREATE TABLE temp01
(
    SEQ         int,
    cat01       numeric(6,0),
    cat02       numeric(6,0),
    dt_day      date,
    dt_week     date,
    dt_month    date,
    price       decimal(10,0)
)

INSERT INTO temp01 VALUES (1, 230, 1, '2019-01-01', '2019-01-05', '2019-01-31', 16000)
INSERT INTO temp01 VALUES (2, 230, 1, '2019-01-02', '2019-01-05', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (3, 230, 1, '2019-01-03', '2019-01-05', '2019-01-31', 13000)
INSERT INTO temp01 VALUES (4, 230, 1, '2019-01-04', '2019-01-05', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (5, 230, 1, '2019-01-05', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (6, 230, 1, '2019-01-06', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (7, 230, 1, '2019-01-07', '2019-01-12', '2019-01-31', 19000)
INSERT INTO temp01 VALUES (1, 230, 2, '2019-01-01', '2019-01-05', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (2, 230, 2, '2019-01-02', '2019-01-05', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (3, 230, 2, '2019-01-03', '2019-01-12', '2019-01-31', 12000)
INSERT INTO temp01 VALUES (4, 230, 2, '2019-01-04', '2019-01-12', '2019-01-31', 17000)
INSERT INTO temp01 VALUES (5, 230, 2, '2019-01-05', '2019-01-12', '2019-01-31', 22000)
INSERT INTO temp01 VALUES (6, 230, 2,' 2019-01-06', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (7, 230, 2,' 2019-01-07', '2019-01-12', '2019-01-31', 21000)

我想从 temp01 得到这个 table。

[结果 01]

    | SEQ | cat01 | cat02 |   dt_day   | price | sub_seq |
    | --- | ----- | ----- | ---------- | ----- | ------- |
    |  1  |  230  |   1   | 2019-01-01 | 16000 |    1    |
    |  3  |  230  |   1   | 2019-01-03 | 13000 |    2    |
    |  7  |  230  |   1   | 2019-01-07 | 19000 |    3    |
    |  3  |  230  |   2   | 2019-01-03 | 12000 |    1    |
    |  4  |  230  |   2   | 2019-01-04 | 17000 |    2    |    
    |  5  |  230  |   2   | 2019-01-05 | 22000 |    3    | 
    |  7  |  230  |   2   | 2019-01-07 | 21000 |    4    |

.....

所以,我使用了这段代码。我认为此代码不正确。

WITH ROW_VALUE AS
(
    SELECT SEQ
        , dt_day
        , cat01
        , cat02
        , price
        , ROW_NUMBER() OVER (ORDER BY cat01, cat02, dt_day) AS sub_seq
    FROM (
        SELECT SEQ
            , cat01
            , cat02
            , dt_day
            , dt_week
            , dt_month
            , price
        FROM temp01
        WHERE price IS NOT NULL
            )val
)
SELECT DISTINCT *
FROM ROW_VALUE
ORDER BY cat01, cat02, dt_day

我怎样才能得到结果01table?

请查看我的代码。

你应该 PARTITION BY cat01, cat02ORDER BY dt_day:

SELECT SEQ
    , cat01
    , cat02
    , dt_day
    , dt_week
    , dt_month
    , price
    , ROW_NUMBER() OVER (PARTITION BY cat01, cat02 ORDER BY dt_day) AS sub_seq
FROM temp01
WHERE price IS NOT NULL

参见demo
结果:

SEQ cat01 cat02 dt_day dt_week dt_month price sub_seq
1 230 1 2019-01-01 2019-01-05 2019-01-31 16000 1
3 230 1 2019-01-03 2019-01-05 2019-01-31 13000 2
7 230 1 2019-01-07 2019-01-12 2019-01-31 19000 3
3 230 2 2019-01-03 2019-01-12 2019-01-31 12000 1
4 230 2 2019-01-04 2019-01-12 2019-01-31 17000 2
5 230 2 2019-01-05 2019-01-12 2019-01-31 22000 3
7 230 2 2019-01-07 2019-01-12 2019-01-31 21000 4