in Education by
I have a rating system in which any person may review other. Each person can be judged by one person more than once. For the calculation of averages, I would like to include only the most current values. Is this possible with SQL? Person 1 rates Person 2 with 5 on 1.2.2011 <- ignored because there is a newer rating of person 1 Person 1 rates Person 2 with 2 on 1.3.2011 Person 2 rates Person 1 with 6 on 1.2.2011 <-- ignored as well Person 2 rates Person 1 with 3 on 1.3.2011 Person 3 rates Person 1 with 5 on 1.5.2011 Result: The Average for Person 2 is 2. The Average for Person 1 is 4. The table may look like this: evaluator, evaluatee, rating, date. Kind Regards Michael JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It's perfectly possible. Let's assume your table structure looks like this: CREATE TABLE [dbo].[Ratings]( [Evaluator] varchar(10), [Evaluatee] varchar(10), [Rating] int, [Date] datetime ); and the values like this: INSERT INTO Ratings SELECT 'Person 1', 'Person 2', 5, '2011-02-01' UNION SELECT 'Person 1', 'Person 2', 2, '2011-03-01' UNION SELECT 'Person 2', 'Person 1', 6, '2011-02-01' UNION SELECT 'Person 2', 'Person 1', 3, '2011-03-01' UNION SELECT 'Person 3', 'Person 1', 5, '2011-05-01' Then the average rating for Person 1 is: SELECT AVG(Rating) FROM Ratings r1 WHERE Evaluatee='Person 1' and not exists (SELECT 1 FROM Ratings r2 WHERE r1.Evaluatee = r2.Evaluatee AND r1.evaluator=r2.evaluator AND r1.date < r2.date) Result: 4 Or for all Evaluatee's, grouped by Evaluatee: SELECT Evaluatee, AVG(Rating) FROM Ratings r1 WHERE not exists (SELECT 1 FROM Ratings r2 WHERE r1.Evaluatee = r2.Evaluatee AND r1.evaluator = r2.evaluator AND r1.date < r2.date) GROUP BY Evaluatee Result: Person 1 4 Person 2 2 This might look like it has an implicit assumption that no entries exist with the same date; but that's actually not a problem: If such entries can exist, then you can not decide which of these was made later anyway; you could only choose randomly between them. Like shown here, they are both included and averaged - which might be the best solution you can get for that border case (although it slightly favors that person, giving him two votes). To avoid this problem altogether, you could simply make Date part of the primary key or a unique index - the obvious primary key choice here being the columns (Evaluator, Evaluatee, Date).

Related questions

0 votes
    I have a rating system in which any person may review other. Each person can be judged by one person more than once. For the ... SQL? Person 1 rates Person 2 with 5 on 1.2.2011...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I try to select the Products that has a yearmodel between +1 and -1 the current year. And I only ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I try to select the Products that has a yearmodel between +1 and -1 the current year. And I only ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This is a question I asked on another forum which received some decent answers, but I wanted to see ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a stored procedure in SQL 2005. The Stored Procedure is actually creating temporary tables in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I have a stored procedure in SQL 2005. The Stored Procedure is actually creating temporary tables in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
0 votes
    I have a stored procedure in SQL 2005. The Stored Procedure is actually creating temporary tables in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
0 votes
    I'm trying to write a query that will pull back the two most recent rows from the Bill table ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Just wondering if any of you guys use Count(1) over Count(*) and if there is a noticeable difference in ... SQL Server 2005.) Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I am using Classic ASP and have the following VB script code From8to14 = date-14 To8to14 = date-8 This sets the two ... "& Day(From8to14) &"' " & _ "AND INTRAY.DateMatched...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I have a table in which data is been logged in 'yy/MM/dd HH:mm:ss' format and my regional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I have a store to get a list of something in system. To get information by business requirement, this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    In SQL Server 2005, we can create temp tables one of two ways: declare @tmp table (Col1 int, Col2 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
...