如何在最小和最大范围之间进行选择

时间:2016-06-27 15:02:19

标签: sql sql-server

我希望在他们参加的联赛中产生随机球员权重。当只有一个联赛Weighting(这是每个联盟的maimun联赛加权)时,我有这个工作,但我想包括MinLeagueWeighting以及所以它从那个数字开始而不是0。

我只需要在疤痕函数中的select语句上获得帮助,以便能够随机选择最小值和最大值之间的加权球员:

下面是league_insert proc,用于确定每个联盟的最大和最小权重:

UPDATE League
SET MaxLeagueWeight =
(
    CASE
        WHEN LeagueID = 1 THEN 90
        WHEN LeagueID = 2 THEN 80
        WHEN LeagueID = 3 THEN 70
        WHEN LeagueID = 4 THEN 85
        WHEN LeagueID = 5 THEN 90
    ELSE 0
    END
    ),
    MinLeagueWeight =
    (
    CASE
        WHEN LeagueID = 1 THEN 50
        WHEN LeagueID = 2 THEN 40
        WHEN LeagueID = 3 THEN 30
        WHEN LeagueID = 4 THEN 45
        WHEN LeagueID = 5 THEN 50
    ELSE 0
    END
    )

以下是执行随机的视图:

CREATE VIEW Random
AS

SELECT RAND() RandValue

以下是需要为最低和最高联赛加权之间的每位球员选择球员加权的疤痕功能(这是我认为需要修复的地方):

CREATE FUNCTION [dbo].[fn_PlayerWeighting]
(   
    @MaxPlayerWeighting INT,
    @MinPlayerWeighting INT
)
RETURNS INT
AS
BEGIN

    DECLARE @playerWeight INT

    SELECT  @playerWeight = ROUND(( ( @MaxPlayerWeighting - 0 - 1 ) * (SELECT RandValue FROM Random) + 0 ), 0)

    RETURN @playerWeight;


END

以下是正在更新和选择的功能:

UPDATE Player
SET PlayerWeighting = dbo.fn_PlayerWeighting (l.MaxLeagueWeight, l.MinLeagueWeight)
FROM Player p
INNER JOIN Team t
    ON t.TeamID = p.TeamID
INNER JOIN League l
    ON l.LeagueID = t.LeagueID



    SELECT dbo.fn_PlayerWeighting (l.MaxLeagueWeight, l.MinLeagueWeight) 
FROM Player p
INNER JOIN Team t
    ON t.TeamID = p.TeamID
INNER JOIN League l
    ON l.LeagueID = t.LeagueID

1 个答案:

答案 0 :(得分:0)

在你的功能中使用它。

SELECT  @playerWeight = ROUND(((@MaxPlayerWeighting - @MinPlayerWeighting -1) * RAND() + @MinPlayerWeighting ), 0)