2016-02-12 29 views
1

我試圖執行括號的查詢,例如8,16,32,64團隊單個消除括號。我想知道給出的種子和比賽的開局以及誰贏得了前幾場比賽,誰在玩誰。試圖在Sql Server中執行遞歸查詢

我已經表定義爲:

CREATE TABLE [dbo].[MatchGame] ( 
    [MatchGameId]   BIGINT IDENTITY (1, 1) NOT NULL, 
    [MatchSizeId]   BIGINT NULL, 
    [StartSeedTeamA]   INT  NULL, 
    [StartSeedTeamB]   INT  NULL, 
    [DateEntered]   DATETIME NULL, 
    [DateUpdated]   DATETIME NULL, 
    [MatchNumber]   INT  NULL, 
    [WinnerPlaysMatchNumber] INT  NULL, 
    [RoundNumber]   INT  NULL, 
    PRIMARY KEY CLUSTERED ([MatchGameId] ASC), 
    CONSTRAINT [FK_MatchGame_MatchSize] FOREIGN KEY ([MatchSizeId]) REFERENCES [dbo].[MatchSize] ([MatchSizeId]) 
); 

的MatchSizeId僅有FK到在它與支架尺寸和描述另一個表。討論並不重要。值StartSeedTeamA和StartSeedTeamB的值爲1以匹配大小(16,32,64等)。種子值在第一輪中有數字,但在後續輪次中爲空。 matchnumber列的值爲1,當隊16參加隊1時,2當隊8參加隊9時,等等。WinnerPlayMatchNumber列將使比賽1的獲勝者在比賽17中獲得比賽2的勝利者。表示比賽回合的值。在16支球隊的單敗淘汰賽中,只有1-4個可能的數值(第一輪比賽,第二輪比賽,第三輪比賽,第四輪比賽)。

我想知道的是,如果給定一個matchsizeid值,我希望能夠查詢一個matchnumber,並獲得可以在遊戲中播放的種子數的允許值。我從另一張桌子知道誰贏了比賽。我希望能寫一個sql server cte來做到這一點,但我的sql fu很弱。我已經嘗試了類似以下的內容,但要知道。

with matchgames_cte (StartSeedTeamA, StartSeedTeamB, MatchNumber, 
    MatchSizeId, WinnerPlaysMatchNumber, RoundNumber) 
as 
( 
    select StartSeedTeamA, StartSeedTeamB, MatchNumber, MatchSizeId, 
    WinnerPlaysMatchNumber, RoundNumber 
    from MatchGame where RoundNumber = 1 
    UNION ALL 
    select a.StartSeedTeamA, a.StartSeedTeamB, a.MatchNumber, a.MatchSizeId, 
    a.WinnerPlaysMatchNumber, a.roundNumber 
    from MatchGame a INNER JOIN matchgames_cte b on a.WinnerPlaysMatchNumber=b.matchnumber where a.StartSeedTeamA is null 
) 

我正試圖用下面的查詢來調用它。 select * from matchgames_cte其中matchnumber = 13和matchsizeid = 3

不幸的是,除了第一輪結果之外,我對cte的任何查詢似乎都沒有給出任何結果。任何建議表示讚賞。謝謝你的時間。

沃利

這裏是被請求的一些數據:

SET IDENTITY_INSERT [dbo].[MatchGame] ON 

GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (1, 1, 1, 4, CAST(N'2016-02-11 21:48:27.370' AS DateTime), CAST(N'2016-02-12 16:03:45.100' AS DateTime), 1, 3, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (2, 1, 2, 3, CAST(N'2016-02-11 21:48:37.863' AS DateTime), CAST(N'2016-02-12 16:03:46.827' AS DateTime), 2, 3, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (3, 1, NULL, NULL, CAST(N'2016-02-11 21:48:45.917' AS DateTime), CAST(N'2016-02-12 16:03:53.080' AS DateTime), 3, NULL, 2) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (4, 2, 1, 8, CAST(N'2016-02-11 21:54:40.783' AS DateTime), CAST(N'2016-02-12 16:03:55.240' AS DateTime), 1, 5, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (5, 2, 5, 4, CAST(N'2016-02-11 21:54:59.317' AS DateTime), CAST(N'2016-02-12 16:03:56.013' AS DateTime), 2, 5, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (6, 2, 3, 6, CAST(N'2016-02-11 21:55:17.287' AS DateTime), CAST(N'2016-02-12 16:03:56.930' AS DateTime), 3, 6, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (7, 2, 2, 7, CAST(N'2016-02-11 21:55:45.730' AS DateTime), CAST(N'2016-02-12 16:03:57.710' AS DateTime), 4, 6, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (8, 2, NULL, NULL, CAST(N'2016-02-11 21:56:12.327' AS DateTime), CAST(N'2016-02-12 16:03:58.883' AS DateTime), 5, 7, 2) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (9, 2, NULL, NULL, CAST(N'2016-02-11 21:56:30.340' AS DateTime), CAST(N'2016-02-12 16:03:59.817' AS DateTime), 6, 7, 2) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (10, 2, NULL, NULL, CAST(N'2016-02-11 21:56:43.483' AS DateTime), CAST(N'2016-02-12 16:04:02.397' AS DateTime), 7, NULL, 3) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (11, 3, 1, 16, CAST(N'2016-02-11 22:12:16.467' AS DateTime), CAST(N'2016-02-12 16:04:06.040' AS DateTime), 1, 9, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (12, 3, 8, 9, CAST(N'2016-02-11 22:12:32.533' AS DateTime), CAST(N'2016-02-12 16:04:06.630' AS DateTime), 2, 9, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (13, 3, 4, 13, CAST(N'2016-02-11 22:12:50.027' AS DateTime), CAST(N'2016-02-12 16:04:07.197' AS DateTime), 3, 10, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (14, 3, 5, 12, CAST(N'2016-02-11 22:13:06.283' AS DateTime), CAST(N'2016-02-12 16:04:08.087' AS DateTime), 4, 10, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (15, 3, 3, 14, CAST(N'2016-02-11 22:13:30.960' AS DateTime), CAST(N'2016-02-12 16:04:08.880' AS DateTime), 5, 11, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (16, 3, 6, 11, CAST(N'2016-02-11 22:13:42.627' AS DateTime), CAST(N'2016-02-12 16:04:09.787' AS DateTime), 6, 11, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (17, 3, 7, 10, CAST(N'2016-02-11 22:14:09.203' AS DateTime), CAST(N'2016-02-12 16:04:11.563' AS DateTime), 7, 12, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (18, 3, 15, 2, CAST(N'2016-02-11 22:14:22.803' AS DateTime), CAST(N'2016-02-12 16:04:13.473' AS DateTime), 8, 12, 1) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (19, 3, NULL, NULL, CAST(N'2016-02-11 22:14:41.130' AS DateTime), CAST(N'2016-02-12 16:04:18.120' AS DateTime), 9, 13, 2) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (20, 3, NULL, NULL, CAST(N'2016-02-11 22:14:48.930' AS DateTime), CAST(N'2016-02-12 16:04:18.777' AS DateTime), 10, 13, 2) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (21, 3, NULL, NULL, CAST(N'2016-02-11 22:15:01.407' AS DateTime), CAST(N'2016-02-12 16:04:19.493' AS DateTime), 11, 14, 2) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (22, 3, NULL, NULL, CAST(N'2016-02-11 22:15:13.843' AS DateTime), CAST(N'2016-02-12 16:04:20.443' AS DateTime), 12, 14, 2) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (23, 3, NULL, NULL, CAST(N'2016-02-11 22:15:23.607' AS DateTime), CAST(N'2016-02-12 16:04:23.297' AS DateTime), 13, 15, 3) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (24, 3, NULL, NULL, CAST(N'2016-02-11 22:15:31.050' AS DateTime), CAST(N'2016-02-12 16:04:24.237' AS DateTime), 14, 15, 3) 
GO 
INSERT [dbo].[MatchGame] ([MatchGameId], [MatchSizeId], [StartSeedTeamA], [StartSeedTeamB], [DateEntered], [DateUpdated], [MatchNumber], [WinnerPlaysMatchNumber], [RoundNumber]) VALUES (25, 3, NULL, NULL, CAST(N'2016-02-11 22:15:36.730' AS DateTime), CAST(N'2016-02-12 16:04:25.867' AS DateTime), 15, NULL, 4) 
GO 
SET IDENTITY_INSERT [dbo].[MatchGame] OFF 
GO 

下面是一個例子查詢我試圖運行:

with matchgames_cte (StartSeedTeamA, StartSeedTeamB, MatchNumber, 
    MatchSizeId, WinnerPlaysMatchNumber, RoundNumber) 
as 
(
    select StartSeedTeamA, StartSeedTeamB, MatchNumber, MatchSizeId, 
    WinnerPlaysMatchNumber, RoundNumber 
    from MatchGame where WinnerPlaysMatchNumber is null 
    UNION ALL 
    select a.StartSeedTeamA, a.StartSeedTeamB, a.MatchNumber, a.MatchSizeId, 
    a.WinnerPlaysMatchNumber, a.roundNumber 
    from MatchGame a INNER JOIN matchgames_cte b on  b.MatchNumber=a.WinnerPlaysMatchNumber where Not(a.WinnerPlaysMatchNumber is null) 
) 
select * from matchgames_cte where StartSeedTeamA is not null and MatchNumber=14 
+0

你能張貼一些樣本數據嗎?如果您發佈了您正在嘗試的實際查詢,這也會有所幫助。 –

+0

您是否嘗試將連接條件從'a.WinnerPlaysMatchNumber = b.matchnumber'切換到'b.WinnerPlaysMatchNumber = a.matchnumber'?我認爲'WinnerPlaysMatchNumber'應該來自CTE並且加入到'MatchRound'表中的匹配號。 –

+0

謝謝肖恩。我添加了一些數據,雖然在這個問題上看起來很糟糕。爲此道歉。韋恩,我也嘗試過這種改變,沒有運氣。 –

回答

0

看起來你的遞歸是正確的,但在最後的WHERE子句中,您嘗試:

  • 通過MatchNumber它看起來像應該是每一個分層獨特的過濾網(我不認爲只有一個MatchNumber = 13行,另一個用於MatchNumber = 14)
  • 過濾器由StartSeedTeamA是NULL幾乎無處不在您的樣本數據

也許這將有助於找到種子隊伍,爲下一場比賽:

select next.MatchNumber, prior.StartSeedTeamA, prior.StartSeedTeamB, prior.MatchNumber as FinishedMatchNumber 
from MatchGame next 
inner join MatchGame prior on next.MatchNumber = prior.WinnerPlaysMatchNumber 
where next.StartSeedTeamA is NULL and prior.StartSeedTeamA is not NULL 
0

我一直在看這個,最後用什麼,我認爲就是答案上來。我把它粘貼在下面。這是一個SQL服務器功能,我用這個cte寫了這個。任何建議讚賞有關改進。謝謝你的時間。

CREATE FUNCTION [dbo].[fn_StartSeeds] 
( 
    @matchsizeid bigint, 
    @startmatchnumber int 
) 
RETURNS TABLE AS RETURN 
( 
    with matchgames_cte (StartSeedTeamA, StartSeedTeamB, MatchNumber, MatchSizeId, winnerplaysmatchnumber) 
    as 
    ( 
     select StartSeedTeamA, StartSeedTeamB, MatchNumber, MatchSizeId, WinnerPlaysMatchNumber 
     from MatchGame where MatchNumber = @startmatchnumber and MatchSizeId = @matchsizeid 
     UNION ALL 
     select a.StartSeedTeamA, a.StartSeedTeamB, a.MatchNumber, a.MatchSizeId, a.WinnerPlaysMatchNumber 
     from MatchGame a JOIN matchgames_cte b on b.MatchNumber=a.WinnerPlaysMatchNumber where a.WinnerPlaysMatchNumber is not null and a.MatchSizeId = @matchsizeid 
    ) 
    select * from matchgames_cte where [email protected] and StartSeedTeamA is not null 
)