2017-10-15 53 views
0
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 

我試圖做SQL:最小值 - 我不想讓不適合的條件,結果

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或1×2 我不想得到它。 我想要一個結果。

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

我沒有看到你想要的結果與你的病情有什麼關係。在那裏做什麼是「ou」/「4」?那不符合你的條件。 –

+0

「我不想導入,除非seqnum是1,ig_game_type是12或1x2。」但是你想要的結果包含seq num,而不是上面提到的語句中的1和game_type。請糾正陳述或期望的結果。 –

+0

你在找這個嗎? (from Info_Game)AS t ORDER BY ....' - >'from Info_Game)as t WHERE NOT(seqnum = 1 AND ig_game_type IN('12','1x2')ORDER BY ...' – Alex

回答

2

也許在OVER()子句中引入一個case表達式會產生你想要的序列? CASE WHEN ig_game_type IN ('12', '1x2') THEN 0 ELSE 1 END

看到它SQL Fiddle

MS SQL服務器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 |