1

我用谷歌搜索并搜索了这个网站,但似乎没有任何效果,似乎它不喜欢我发现的所有内容的语法。虽然看起来很简单,但它变得令人沮丧。谁能帮忙?希望它使用我创建的 topRange 和 bottomRange 变量

ALTER PROCEDURE [dbo].[getTopFantasyPlayersByPositionSeason]
    @top int,
    @position varchar(3),
    @topRange int,
    @bottomRange int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    IF @position = 'QB' OR @position = 'RB' OR @position = 'WR' OR @position = 'TE' OR @position = 'K' OR @position = 'D' OR @position = 'TE'
        BEGIN
            SELECT TOP (@top) *
            FROM nflPassers_season
            WHERE position=@position AND points > 0
            ORDER BY points DESC
        END

END
4

2 回答 2

1
ALTER PROCEDURE [dbo].[getTopFantasyPlayersByPositionSeason]
    @top INT
  , @position VARCHAR(3)
  , @topRange INT
  , @bottomRange INT
AS 
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
        SET NOCOUNT ON;

    -- Insert statements for procedure here
        IF @position IN ( 'QB', 'RB', 'WR', 'TE', 'K', 'D', 'TE' ) 
            BEGIN
            ;
                WITH    c AS ( SELECT   rn = ROW_NUMBER() OVER ( ORDER BY points DESC)
                                      , firstname
                               FROM     nflPassers_season
                               WHERE    position = @position
                                        AND points > 0
                             )
                    SELECT  firstname
                    FROM    c
                    WHERE   rn BETWEEN @toprange AND @bottomRange

            END

    END
于 2013-08-09T18:04:31.473 回答
1

SQL 2012 之前的基本分页:

SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY points DESC) AS RowRank
      FROM YourTable
      )AS sub
WHERE Rowrank BETWEEN @Variable1 AND @Variable2

如果您想要每个分组的结果范围,即按玩家,您可以添加PARTITION BYROW_NUMBER()

SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY SomeField ORDER BY points DESC) AS RowRank
      FROM YourTable
      WHERE position = @position
        AND points > 0
      ) AS sub
WHERE Rowrank BETWEEN @Variable1 AND @Variable2

这将使该PARTITION BY子句中每个项目的排名从 1 开始。

于 2013-08-09T18:04:31.687 回答