如何只导入符合条件的行?

时间:2017-10-16 10:42:16

标签: 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 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

结果:

SQL Fiddle

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 (1, 3, 11, '12', 1),
       (2, 3, 11, 'ah', 2),
       (3, 3, 11, 'ou', 3),
       (4, 1, 12, '12', 1),
       (5, 4, 13, '1h_ah', 1), 
       (6, 4, 13, '2h_ou', 2),
       (7, 4, 13, 'ah', 3),
       (8, 4, 13, 'ou', 4),
       (9, 4, 13, '1h_ou', 5),
       (10, 5, 14, '1x2', 1),
       (11, 5, 14, '1h_ah', 2),
       (12, 5, 14, 'h', 3),
       (13, 5, 14, 'ou', 4),
       (14, 5, 14, '1h_ou', 5);

只有ig_root,其中seqnum为1,ig_game_type为'12'或'1x2'。

我想获得相同数字的ig_root

我想要结果

SQL Fiddle

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 (1, 3, 11, '12', 1),
       (2, 3, 11, 'ah', 2),
       (3, 3, 11, 'ou', 3),
       (4, 1, 12, '12', 1),
       (10, 5, 14, '1x2', 1),
       (11, 5, 14, '1h_ah', 2),
       (12, 5, 14, 'h', 3),
       (13, 5, 14, 'ou', 4),
       (14, 5, 14, '1h_ou', 5);

相同数量的ig_root,seqnum = 1和type = '12'或type ='1x2' 我想得到相同数量的ig_root

ig_idx  ig_root ig_game_type  seqnum
1       11      12            1
2       11      ah            2
3       11      ou            3
4       12      12            1
5       13      1h_ah         1  <--- this 
9       13      1h_ou         2
6       13      2h_ou         3
7       13      ah            4
8       13      ou            5
10      14      1x2           1
11      14      1h_ah         2
14      14      1h_ou         3
12      14      h             4
13      14      ou            5

idx 5:这意味着seqnum为1, 但类型不是12或1x2。 所以我不想带ig_root 13号。

像这样:

ig_idx  ig_root ig_game_type  seqnum
1       11      12            1
2       11      ah            2
3       11      ou            3
4       12      12            1
10      14      1x2           1
11      14      1h_ah         2
14      14      1h_ou         3
12      14      h             4
13      14      ou            5

3 个答案:

答案 0 :(得分:2)

with cte as (
      SELECT
          *
          , ROW_NUMBER() OVER (PARTITION BY ig_root ORDER BY ig_idx) AS seqnum
      FROM Info_Game
)
select ig1.*
from Info_Game ig1
where exists (
        select *
        from cte ig2
        where ig2.ig_root = ig1.ig_root
            and ig2.ig_game_type in ('12', '1x2')
            and ig2.seqnum = 1
    )

这将返回Info_Game中的所有行,其中存在一行,其中ig_game_type为&#39; 12&#39;或&#39; 1x2&#39;并且是具有相同ig_root值的所有行的第一行。 &#39; first&#39;的定义我的问题并不清楚,所以我冒昧地使用ig_idx进行排序。 SQLFiddle

答案 1 :(得分:0)

如果您只想输出(或保留)其中一些行,请使用WHERE clause,如下所示:

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
WHERE t.seqnum in (1,2)
ORDER BY ig_root, seqnum
;
  

只有ig_root,seqnum为1,ig_game_type为'12'或'1x2'

我完全不明白这些是WHERE clause替代

WHERE t.seqnum = 1

WHERE ig_game_type n('12','1x2')

我希望这会有所帮助。

答案 2 :(得分:0)

难以解码您的问题,但根据我的想法,您希望从您发布的查询中获取几行。所以只需使用where子句即WHERE ig_game_type in('12','1x2') and seqnum = 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
WHERE  ig_game_type in('12','1x2') and seqnum = 1
ORDER BY ig_root, seqnum