查找每位客户的每个类别的最新评分。

huangapple go评论93阅读模式
英文:

Find the latest rating for each category for each customer

问题

以下是要翻译的SQL代码的部分:

  1. select
  2. coalesce(a.StudentID,b.StudentID) as StudentID,
  3. a.rating as RAPID_Rating,
  4. RAPID_Games,
  5. b.rating as BLITZ_Rating,
  6. BLITZ_Games,
  7. RAPID_Games + BLITZ_Games as Total_Games
  8. from
  9. (select
  10. StudentID,
  11. ScoreType,
  12. rating,
  13. count(rating) over(partition by StudentID,ScoreType) as RAPID_Games,
  14. row_number() over(partition by StudentID,ScoreType order by date desc) as RNUM
  15. from rating
  16. where ScoreType ='RAPID'
  17. ) a join (
  18. select
  19. StudentID,
  20. ScoreType,
  21. rating,
  22. count(rating) over(partition by StudentID,ScoreType) as BLITZ_Games,
  23. row_number() over(partition by StudentID,ScoreType order by date desc) as RNUM
  24. from rating
  25. where ScoreType ='BLITZ'
  26. ) b on a.StudentID = b.StudentID
  27. where a.RNUM = 1 and b.RNUM = 1

希望这对你有所帮助。

英文:

The data in the rating table looks like this:

StudentID Rating ScoreType Date
1 12 RAPID 2023-01-01
1 15 RAPID 2023-01-10
1 20 RAPID 2023-02-15
1 25 RAPID 2023-02-20
1 25 BLITZ 2023-03-01
1 33 BLITZ 2023-03-20
2 17 RAPID 2023-01-15
2 19 BLITZ 2023-02-06

What I am trying to build is like this:

StudentID RAPID_Rating RAPID_Games BLITZ_Rating BLITZ_Games Total Games
1 25 4 33 2 6
2 17 1 19 1 2

The data should be one row for each student

Column definitions of the expected output:

RAPID_Rating : Latest rating value based on date where ScoreType = 'RAPID'

RAPID_Games: Total number of RAPID games played by each student

BLITZ_Rating: Latest rating value based on date where ScoreType = 'BLITZ'

BLITZ_Games: Total number of BLIRZ games played by each student

Total Games: Total games played by each student

The SQL Code that I have tried:

  1. select
  2. coalesce(a.StudentID,b.StudentID) as StudentID,
  3. a.rating as RAPID_Rating,
  4. RAPID_Games,
  5. b.rating as BLITZ_Rating,
  6. BLITZ_Games,
  7. RAPID_Games + BLITZ_Games as Total_Games
  8. from
  9. (select
  10. StudentID,
  11. ScoreType,
  12. rating,
  13. count(rating) over(partition by StudentID,ScoreType) as RAPID_Games,
  14. row_number() over(partition by StudentID,ScoreType order by date desc) as RNUM
  15. from rating
  16. where ScoreType ='RAPID'
  17. ) a join (
  18. select
  19. StudentID,
  20. ScoreType,
  21. rating,
  22. count(rating) over(partition by StudentID,ScoreType) as BLITZ_Games,
  23. row_number() over(partition by StudentID,ScoreType order by date desc) as RNUM
  24. from rating
  25. where ScoreType ='BLITZ'
  26. ) b on a.StudentID = b.StudentID
  27. where a.RNUM = 1 and b.RNUM = 1

I have to do this calculation for another 2 categories then that will add up another 2 sub queries. Is there anyway to optimize this SQL code?

答案1

得分: 2

这里我们可以使用 ROW_NUMBER 与条件聚合:

  1. WITH cte AS (
  2. SELECT *, ROW_NUMBER() OVER (PARTITION BY StudentID, ScoreType ORDER BY Date DESC) rn
  3. FROM rating
  4. )
  5. SELECT
  6. StudentID,
  7. MAX(Rating) FILTER (WHERE ScoreType = 'RAPID' AND rn = 1) AS RAPID_Rating,
  8. COUNT(*) FILTER (WHERE ScoreType = 'RAPID') AS RAPID_Games,
  9. MAX(Rating) FILTER (WHERE ScoreType = 'BLITZ' AND rn = 1) AS BLITZ_Rating,
  10. COUNT(*) FILTER (WHERE ScoreType = 'BLITZ') AS BLITZ_Games,
  11. COUNT(*) AS "Total Games"
  12. FROM cte
  13. GROUP BY StudentID
  14. ORDER BY StudentID;
英文:

We can use ROW_NUMBER along with conditional aggregation here:

<!-- language: sql -->

  1. WITH cte AS (
  2. SELECT *, ROW_NUMBER() OVER (PARTITION BY StudentID, ScoreType ORDER BY Date DESC) rn
  3. FROM rating
  4. )
  5. SELECT
  6. StudentID,
  7. MAX(Rating) FILTER (WHERE ScoreType = &#39;RAPID&#39; AND rn = 1) AS RAPID_Rating,
  8. COUNT(*) FILTER (WHERE ScoreType = &#39;RAPID&#39;) AS RAPID_Games,
  9. MAX(Rating) FILTER (WHERE ScoreType = &#39;BLITZ&#39; AND rn = 1) AS BLITZ_Rating,
  10. COUNT(*) FILTER (WHERE ScoreType = &#39;BLITZ&#39;) AS BLITZ_Games,
  11. COUNT(*) AS &quot;Total Games&quot;
  12. FROM cte
  13. GROUP BY StudentID
  14. ORDER BY StudentID;

huangapple
  • 本文由 发表于 2023年6月5日 00:02:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定