SQL:最小值 - 我不希望得到不符合条件的结果

时间:2017-10-15 19:48:28

标签: sql sql-server

SELECT t.*
from (
    select ig_idx, count(ig_root) over(partition by ig_root) AS GameCount1,
        ig_root,ig_game_type,
        row_number() over (partition by ig_root order by ig_root asc) as seqnum
    from Info_Game) AS t
ORDER BY ig_root,ig_idx ASC

我正在尝试

    ig_idx  GameCount1  ig_root ig_game_type    seqnum
    --------------------------------------------------
    419754  3           2409758   12                1 <-- ig_game_type is 12 or 1x2
    419755  3           2409758   ah                2
    419756  3           2409758   ou                3
    419757  1           2409775   12                1

我不想得到这个

    418265    5           2421498   1h_ah         1 <-- ig_game_type is not 12 or 1x2
    418266    5           2421498   2h_ou         2
    418267    5           2421498   ah            3
    418268    5           2421498   ou            4
    418269    5           2421498   1h_ou         5
    417618  5           2421579   1x2               1  <-- ig_game_type is 12 or 1x2
    417619  5           2421579   1h_ah             2
    417620  5           2421579   ah                3
    417621  5           2421579   ou                4
    417622  5           2421579   1h_ou             5

如果seqnum为1但不是ig_game_type 12或1x2 我不想得到它。 我想要一个结果。

ig_idx  GameCount1  ig_root ig_game_type    seqnum
--------------------------------------------------
419754  3           2409758 12              1
419755  3           2409758 ah              2
419756  3           2409758 ou              3
419757  1           2409775 12              1
417618  5           2421579 1x2             1
417619  5           2421579 1h_ah           2
417620  5           2421579 h               3
417621  5           2421579 ou              4
417622  5           2421579 1h_ou           5

1 个答案:

答案 0 :(得分:2)

或许在OVER()子句中引入一个case表达式会产生你想要的序列吗? CASE WHEN ig_game_type IN ('12', '1x2') THEN 0 ELSE 1 END

SQL Fiddle

看到它

MS SQL Server 2014架构设置

CREATE TABLE Info_Game
    ([ig_idx] int, [GameCount1] int, [ig_root] int, [ig_game_type] varchar(5), [old_seqnum] int)
;

INSERT INTO Info_Game
    ([ig_idx], [GameCount1], [ig_root], [ig_game_type], [old_seqnum])
VALUES
    (419754, 3, 2409758, '12', 1),
    (419755, 3, 2409758, 'ah', 2),
    (419756, 3, 2409758, 'ou', 3),
    (419757, 1, 2409775, '12', 1),
    (417618, 5, 2421579, '1x2', 1),
    (417619, 5, 2421579, '1h_ah', 2),
    (417620, 5, 2421579, 'h', 3),
    (417621, 5, 2421579, 'ou', 4),
    (417622, 5, 2421579, '1h_ou', 5)
;

查询1

SELECT
      t.*
FROM (
      SELECT
            ig_idx
            --, count(ig_root) over(partition by ig_root) AS GameCount1
          , ig_root
          , ig_game_type
          , ROW_NUMBER() OVER (PARTITION BY ig_root
                              ORDER BY CASE WHEN ig_game_type IN ('12', '1x2') THEN 0 ELSE 1 END
                              , ig_game_type ASC
                              ) AS seqnum
      FROM Info_Game
      ) AS t
ORDER BY ig_root, seqnum

<强> Results

| ig_idx | ig_root | ig_game_type | seqnum |
|--------|---------|--------------|--------|
| 419754 | 2409758 |           12 |      1 |
| 419755 | 2409758 |           ah |      2 |
| 419756 | 2409758 |           ou |      3 |
| 419757 | 2409775 |           12 |      1 |
| 417618 | 2421579 |          1x2 |      1 |
| 417619 | 2421579 |        1h_ah |      2 |
| 417622 | 2421579 |        1h_ou |      3 |
| 417620 | 2421579 |            h |      4 |
| 417621 | 2421579 |           ou |      5 |