Sql query to determine when threshold was reached

a 夏天 提交于 2021-02-08 06:59:52

问题


I have an interesting SQL problem I'd appreciate some suggestions on.

I have a table with the columns:

DateAdded
Score
Team

Users will enter their own scores however they will not necessarily be in order (some may be back dated if they do not use the system each day).

Each member of the team's score is added together, the team which reaches a threshold score first wins.

I want a query which will tell me which team reached the threshold first and on what date.


回答1:


I think, this is what you want to achieve.

SELECT TOP 1 T1.Dateadded, T1.Team FROM Table1 T1
 JOIN Table1 T2
  ON T1.Team = T2.Team
    and T1.Dateadded >= T2.Dateadded
GROUP BY T1.Dateadded, T1.Team
HAVING SUM(T2.Score) >= @Threshold
ORDER BY T1.Dateadded 

SQL Fiddle




回答2:


What you need is a cumulative sum. And, SQL Server 2008 doesn't support it. Good news, SQL Server 2012 does.

So, you can do this with a correlated subquery:

select team, min(dateadded) as FirstPastThreshold
from (select dateadded, score, team,
             (select sum(score) from t t2 where t2.team = t.team and t2.dateadded <= t.dateadded) as cumulativescore
      from t
     ) t
where cumulativescore>= @threshhold
group by team



回答3:


You could use DENSE_RANK to determine the best/first team which reaches the threshold.

WITH CTE AS
(
  SELECT 
    DateAdded, Score, Team,
    DENSE_RANK() OVER (Order By DateAdded ASC, Score DESC) AS Rank
  FROM dbo.TableName
  WHERE
    Score >= Threshold
)
SELECT 
  DateAdded, Score, Team
FROM CTE
WHERE
  Rank = 1

Note that this can return multiple teams.




回答4:


You could add a forth column called currentThresholdScore. When ever a new record is inserted the currentThresholdScore column will be the value of that records Score plus any previous records that have been inserted. Then check to see if the currentThresholdScore exceeds the threshold to send a mail etc.




回答5:


You can use a subquery for this problem. Just add a column to the query which lists the current score plus the sum of past scores and then find the first record by date that exceeds the threshold

Sql Fiddle

Use tempdb
Create Table Scoring (DateAdded DateTime, Score INT, Team INT)
INSERT Scoring SELECT GetDate(), 100, 1
INSERT Scoring SELECT GetDate()+1, 150, 1
INSERT Scoring SELECT GetDate()+1, 50, 2
INSERT Scoring SELECT GetDate()+2, 75, 2
INSERT Scoring SELECT GetDate()-10, 75, 2


DECLARE @Threshhold INT
SET @Threshhold = 125

-- Table which includes a cumulative score
;WITH tbl AS 
(
SELECT 
    Team, 
    DateAdded, 
    Score, 
    -- This column calculates the current score + the sum of past scores
    IsNull((SELECT Sum(t2.Score) CumScore FROM Scoring t2 WHERE t2.Team = t.Team AND t2.DateAdded < t.DateAdded),0) 
    + Score AS  CumScore
FROM Scoring t
)

-- Find first record > threshold
SELECT TOP 1 * FROM tbl
WHERE CumScore >= @Threshhold
ORDER BY DateAdded ASC


来源:https://stackoverflow.com/questions/14237492/sql-query-to-determine-when-threshold-was-reached

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!