Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

sql server - Naming a Column using Variable

I wish to calculate Ascending Points Diff for all players in a sweep over a series of games. I wish to have the first column name PD1 with the least Points Difference, PD2 next lowest and so on. I wish to do this using a variable which just adds the integer to the end of 'PD' based on the number of games. I keep getting an error when I add AS @ColumnName to the end of the Calculation.

USE [Rugby Pools]


DECLARE @counter int
DECLARE @MaxPlayer int
DECLARE @ColumnName varchar(50)

SET @counter = (SELECT MIN([Player_ID]) FROM [dbo].[Players])
SET @MaxPlayer = (SELECT MAX([Player_ID]) FROM [dbo].[Players])


DECLARE @gamecounter int
DECLARE @MaxGame int
SET @gamecounter = (SELECT MIN([Game_ID]) FROM [dbo].[Match])
SET @MaxGame = (SELECT MAX([Game_ID]) FROM [dbo].[Match])
SET @ColumnName='PD'+@gamecounter

WHILE @gamecounter <= @MaxGame

BEGIN


WHILE @counter <= @MaxPlayer

BEGIN



SELECT     TOP (@gamecounter)   dbo.Players.Player_ID, dbo.Entries.Game_ID, ABS(ABS(dbo.Entries.Home_Score-dbo.Entries.Away_Score)-(dbo.Match.Home_Score-dbo.Match.Away_Score)) AS @ColumnName
FROM            Entries INNER JOIN
                         Match ON Entries.Game_ID = Match.Game_ID INNER JOIN
                         Players ON Entries.Player_ID = Players.Player_ID

                         WHERE dbo.Match.Home_Score IS NOT NULL AND dbo.Players.Player_ID=@counter

                         ORDER BY Players.Player_ID, PointsDiff1 ASC

SET @Counter += 1

                         END                        

SET @gamecounter += 1

END 

Once working, I will be able to alter it to update a table which will allow me to provide a league table of players decided by wins, followed by best points difference over the course of the tournament.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your final query section to dynamic sql like this. It can be achieved only by dynamic sql.

 declare @query nvarchar(max)
 set @query = ' SELECT TOP (' + cast(@gamecounter as varchar(10)) + ') dbo.Players.Player_ID, 
               dbo.Entries.Game_ID, ABS(ABS(dbo.Entries.Home_Score-dbo.Entries.Away_Score)-(dbo.Match.Home_Score-dbo.Match.Away_Score)) AS ' + QUOTENAME(@ColumnName) + '
    FROM            Entries INNER JOIN
                             Match ON Entries.Game_ID = Match.Game_ID INNER JOIN
                             Players ON Entries.Player_ID = Players.Player_ID

                             WHERE dbo.Match.Home_Score IS NOT NULL AND dbo.Players.Player_ID= ' + cast(@counter as varchar(10)) + '
                             ORDER BY Players.Player_ID, PointsDiff1 ASC'

  sp_executesql @query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...