一些背景可以帮助首先解释这个难题:
我有一个数据库,通过将用户提交的值与全球平均值进行比较来估计用户的可靠性。值介于 0 和 1 之间。因此,其中:
- 此特定用户的可靠性 =
r
- 此特定用户提交值的平均值 =
a
- 全球,“商定”平均值 =
g
可靠性:
r = 1 - ABS(g - a)
这就是计算每个用户的可靠性的方式。现在,“商定”的全局平均值g
是使用加权平均值计算的,其中权重为r
,值为a
。如果总共有 3 个用户:
g = ((r1 * a1) + (r2 * a2) + (r3 * a3)) / (r1 + r2 + r3)
问题是,一旦用户有了高可靠性,他们就完全垄断了,任何新的价值观都无法改变这一点。举个例子:
g was initially 0.5
user1 r was initially 0.5
user2 r was initially 0.5
user3 r was initially 0.5
现在,他们将一一提交值,并观察会发生什么:
user1 a is submitted, 1.0
user1 reliability goes slightly down because it differs from g (0.5)
user2 a is submitted, 1.0
user1 and user2 reliability go up to 100%, g is now 1.0.
user3 a is submitted, 0.0
user3 reliability goes down to 0%. g is still 1.0.
由于 user3 的可靠性非常低,因此加权对 没有任何影响g
。User3 的可靠性下降,因为提交的值与全局平均值完全不同。怎么做才能让 user3 的提交对最终值产生一些影响?也许我需要添加一些常数,以便可靠性永远不会完全为零(但接近)?
现在,对于 SQL 代码。我添加了一个演示问题的 SQL 小提琴: http ://sqlfiddle.com/#!3/d3fd1/21 我已经抽象了代码以使其保持简短,但它仍然很长。
表创建、存储过程和触发器:
-- Stores user info
CREATE TABLE dbo.Users(
[UserID] [int] NOT NULL,
[Reliability] [float] NOT NULL
)
-- Contains global averages from all users who submitted data
CREATE TABLE dbo.GlobalSubmission(
GlobalSubmissionID [int] NOT NULL,
Name [varchar](50) NULL,
GlobalAverage [float] NOT NULL,
)
CREATE TABLE dbo.UserSubmission(
SubValue float NOT NULL,
GlobalSubmissionID int NOT NULL,
UserID int NOT NULL,
)
GO
--Calculate the "ideal value", used for GlobalSubmission.
CREATE FUNCTION dbo.IdealValueCalc(@globalSubmissionID INT)
RETURNS int
AS
BEGIN
DECLARE @tmpReliability TABLE (SubValue float, Reliability float)
INSERT INTO @tmpReliability
SELECT AVG(us.SubValue) as SubValue, usr.Reliability Reliability FROM UserSubmission us
JOIN Users usr
ON us.UserID = usr.UserID
WHERE GlobalSubmissionID = @GlobalSubmissionID
GROUP BY us.UserID, usr.Reliability
--Perform weighted mean calculations.
Return (SELECT SUM(SubValue * Reliability) / SUM(Reliability) FROM @tmpReliability)
END
go
--Calculate the reliability of one user.
CREATE FUNCTION dbo.GetReliabilityForUser
(@userID int)
Returns Float
AS BEGIN
Return (SELECT 1 - AVG(ABS(db.userAvg - db.GlobalAverage))
FROM (
SELECT pmd.UserID,
gs.GlobalAverage,
AVG(pmd.SubValue) as userAvg
FROM UserSubmission pmd
-- Joins average value for each user with "ideal" value from GlobalSubmission
JOIN GlobalSubmission gs
ON gs.GlobalSubmissionID = pmd.GlobalSubmissionID
WHERE pmd.UserID = 1
GROUP BY pmd.UserID, gs.GlobalSubmissionID, gs.GlobalAverage
) db
GROUP BY db.UserID)
End
go
CREATE TRIGGER trg_SubmissionComputation
ON UserSubmission
AFTER INSERT, UPDATE
AS BEGIN
--Calculate this uer's reliability
DECLARE @userID int = (SELECT TOP(1) UserID FROM inserted)
DECLARE @userReliability float = dbo.GetReliabilityForUser(@userID)
UPDATE Users
SET Reliability=@userReliability
WHERE UserID = @userID
--Recalculate globalSubmission values:
DECLARE @globalSubmissionID int = (SELECT TOP(1) GlobalSubmissionID FROM inserted)
DECLARE @globalAverage float = dbo.IdealValueCalc(@globalSubmissionID)
--The global average for this set of submissions has been recalculated. Now inserting:
UPDATE GlobalSubmission
SET GlobalAverage = @globalAverage
WHERE GlobalSubmissionID = @globalSubmissionID
END
GO
测试它:
--Creating 3 new users
INSERT INTO Users
(UserID, Reliability)
values
(1, 0.5),
(2, 0.5),
(3, 0.5)
GO
--Creating a new GlobalSubmission
INSERT INTO GlobalSubmission
(GlobalSubmissionID, NAME, GlobalAverage)
values (1, 'BOILER2B' , 0.5)
GO
--First, we will submit values of 1 for two users:
INSERT INTO UserSubmission values (1.0, 1, 1); -- Value: 1.0, User 1, Submission 1
GO
INSERT INTO UserSubmission values (1.0, 1, 2); -- Value: 1.0, User 2, Submission 1
GO
INSERT INTO UserSubmission values (1.0, 1, 1); -- Value: 1.0, User 1, Submission 1
GO
INSERT INTO UserSubmission values (1.0, 1, 2); -- Value: 1.0, User 2, Submission 1
GO
--Now, we will submit values of 0 for the third user:
INSERT INTO UserSubmission values (0.0, 1, 3); -- Value: 0.0, User 3, Submission 1
GO
INSERT INTO UserSubmission values (0.0, 1, 3); -- Value: 0.0, User 3, Submission 1
GO
SELECT * FROM Users -- This results in 0% reliability for the last user.
--If we create new users and add them, the reliability won't budge:
INSERT INTO Users
(UserID, Reliability)
values
(4, 0.5),
(5, 0.5),
(6, 0.5),
(7, 0.5),
(8, 0.5)
GO
INSERT INTO UserSubmission values (0, 1, 4); -- Value: 0, User 4, Submission 1
GO
INSERT INTO UserSubmission values (0, 1, 5); -- Value: 0, User 5, Submission 1
GO
INSERT INTO UserSubmission values (0, 1, 6); -- Value: 0, User 6, Submission 1
GO
INSERT INTO UserSubmission values (0, 1, 7); -- Value: 0, User 7, Submission 1
GO
INSERT INTO UserSubmission values (0, 1, 8); -- Value: 0, User 8, Submission 1
GO
INSERT INTO UserSubmission values (0, 1, 4); -- Value: 0, User 4, Submission 1
GO
INSERT INTO UserSubmission values (0, 1, 5); -- Value: 0, User 5, Submission 1
GO
INSERT INTO UserSubmission values (0, 1, 6); -- Value: 0, User 6, Submission 1
GO
INSERT INTO UserSubmission values (0, 1, 7); -- Value: 0, User 7, Submission 1
GO
INSERT INTO UserSubmission values (0, 1, 8); -- Value: 0, User 8, Submission 1
GO
SELECT * FROM Users -- Even though we've added loads of new users suggesting 0 as value, the final value
-- is remaining 1.0, because when a new value (0) is submitted, it varies too much from the global average
--(1), causing the reliability of that user to go down, and that user ends up making no influence on the
-- global average!