我想知道柜台的结果

时间:2017-11-03 02:25:00

标签: sql sql-server

我有一个SQL查询,如下所示:

SELECT ig_idx,
               ig_team1,

               Count(ig_root)
                 OVER(
                   partition BY ig_root) GameCount1,
               ig_game_type,
               Row_number()
                 OVER (
                   partition BY ig_root, ig_game_type
                   ORDER BY CASE WHEN ig_game_type IN ('12', '1x2')
                 THEN 0
                 ELSE 1 END
                 , ig_game_type ASC )    AS seqnum
        FROM   info_game

结果是:

ig_idx  ig_team1    GameCount1  ig_game_type seqnum
-------------------------------------------------------
494155  Real Madrid  11         [1Q] ou      1
494152  Real Madrid  11         [1Q] ah      1
494156  Real Madrid  11         [1h] ou      1
494611  Real Madrid  11         [1h] ou      2
494153  Real Madrid  11         [1h] ah      1
494150  Real Madrid  11         12           1
494154  Real Madrid  11         ou           1
494388  Real Madrid  11         ou           2
494473  Real Madrid  11         ou           3
494151  Real Madrid  11         ah           1
495035  Real Madrid  11         ah           2

我只想显示其中一个结果,如果seqnum number,则cnt中的11是行计数器。

WITH cte_example 
AS(
SELECT ig_idx,
               ig_team1,

               Count(ig_root)
                 OVER(
                   partition BY ig_root) GameCount1,
               ig_game_type,
               Row_number()
                 OVER (
                   partition BY ig_root, ig_game_type
                   ORDER BY CASE WHEN ig_game_type IN ('승패', '승무패')
                 THEN 0
                 ELSE 1 END
                 , ig_game_type ASC )    AS seqnum
        FROM   info_game
)
                               SELECT * FROM cte_example WHERE seqnum < 2

结果是:

ig_idx  ig_team1    GameCount1  ig_game_type seqnum
-------------------------------------------------------
494155  Real Madrid  11         [1Q] ou      1
494152  Real Madrid  11         [1Q] ah      1
494156  Real Madrid  11         [1h] ou      1
494153  Real Madrid  11         [1h] ah      1
494150  Real Madrid  11         12           1
494154  Real Madrid  11         ou           1
494151  Real Madrid  11         ah           1

但GameCount1的结果仍然是11。

如何获得行计数器7?

非常感谢!

2 个答案:

答案 0 :(得分:0)

你能检查一下:

WITH cte_example (ig_idx,ig_team1,GameCount1,ig_game_type,seqnum)
AS(
SELECT ig_idx,
               ig_team1,

               Count(ig_root)
                 OVER(
                   partition BY ig_root) GameCount1,
               ig_game_type,
               Row_number()
                 OVER (
                   partition BY ig_root, ig_game_type
                   ORDER BY CASE WHEN ig_game_type IN ('승패', '승무패')
                 THEN 0
                 ELSE 1 END
                 , ig_game_type ASC )    AS seqnum
        FROM   info_game
)
                               SELECT * FROM cte_example WHERE seqnum < 2

答案 1 :(得分:0)

你应该计算出CTE的行数。

;WITH cte_example 
AS(
SELECT ig_idx,
               ig_team1,
               ig_game_type,
               ig_root,
               Row_number()
                 OVER (
                   partition BY ig_root, ig_game_type
                   ORDER BY CASE WHEN ig_game_type IN ('승패', '승무패')
                 THEN 0
                 ELSE 1 END
                 , ig_game_type ASC )    AS seqnum
        FROM   info_game
)
SELECT 
ig_idx,
ig_team1,
Count(ig_root) OVER( partition BY ig_root) GameCount1,
ig_game_type,
seqnum
FROM cte_example WHERE seqnum < 2