rank() 基于 SQL 中特定列条件的排名。

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

rank() based on certain column condition in SQL

问题

我要根据每天的出勤报告来为学生排名。排名应该分别为每个学生开始。以下是预期的输出:

studentid 日期 出勤(Y/N) 排名
3524 01-Jan-2020 N 0
3524 02-Jan-2020 N 0
3524 03-Jan-2020 N 0
3524 04-Jan-2020 Y 1
3524 05-Jan-2020 Y 2
3524 06-Jan-2020 Y 3
3524 07-Jan-2020 N 0
3524 08-Jan-2020 N 0
3524 09-Jan-2020 Y 1
3524 10-Jan-2020 Y 2
3524 11-Jan-2020 Y 3
3524 12-Jan-2020 Y 4
5347 04-Oct-2020 Y 1
5347 05-Oct-2020 Y 2
5347 06-Oct-2020 N 0
5347 07-Oct-2020 N 0
5347 08-Oct-2020 N 0
5347 09-Oct-2020 Y 1
5347 10-Oct-2020 Y 2
5347 11-Oct-2020 Y 3

数据如下:

studentid 开始日期 结束日期 出勤(Y/N)
3524 01-Jan-2020 03-Jan-2020 N
3524 04-Jan-2020 06-Jan-2020 Y
3524 07-Jan-2020 08-Jan-2020 N
3524 09-Jan-2020 12-Jan-2020 Y
5347 04-Oct-2020 05-Oct-2020 Y
5347 06-Oct-2020 08-Oct-2020 N
5347 09-Oct-2020 11-Oct-2020 Y

以下是您提供的代码的翻译,根据日期和学生出勤情况来确定排名。如果学生在特定日期缺席(N),则排名为0,否则给出排名。

CASE 
WHEN [Date] BETWEEN StartDate AND EndDate AND attendance='N' THEN 0
WHEN [Date] BETWEEN StartDate AND EndDate AND attendance='Y'
THEN RANK() OVER (
PARTITION BY studentid,attendance ORDER BY [Date])
ELSE 0
END AS ranking
英文:

I want to rank the students based on their attendance report on each day. The ranking should start for each student separately. Below is the expected output,

Expected output:

studentid Date attendance(Y/N) rank
3524 01-Jan-2020 N 0
3524 02-Jan-2020 N 0
3524 03-Jan-2020 N 0
3524 04-Jan-2020 Y 1
3524 05-Jan-2020 Y 2
3524 06-Jan-2020 Y 3
3524 07-Jan-2020 N 0
3524 08-Jan-2020 N 0
3524 09-Jan-2020 Y 1
3524 10-Jan-2020 Y 2
3524 11-Jan-2020 Y 3
3524 12-Jan-2020 Y 4
5347 04-Oct-2020 Y 1
5347 05-Oct-2020 Y 2
5347 06-Oct-2020 N 0
5347 07-Oct-2020 N 0
5347 08-Oct-2020 N 0
5347 09-Oct-2020 Y 1
5347 10-Oct-2020 Y 2
5347 11-Oct-2020 Y 3

data:

studentid startdate enddate attendance(Y/N)
3524 01-Jan-2020 03-Jan-2020 N
3524 04-Jan-2020 06-Jan-2020 Y
3524 07-Jan-2020 08-Jan-2020 N
3524 09-Jan-2020 12-Jan-2020 Y
5347 04-Oct-2020 05-Oct-2020 Y
5347 06-Oct-2020 08-Oct-2020 N
5347 09-Oct-2020 11-Oct-2020 Y

output I got from the below code:

studentid Date attendance(Y/N) rank
3524 01-Jan-2020 N 0
3524 02-Jan-2020 N 0
3524 03-Jan-2020 N 0
3524 04-Jan-2020 Y 1
3524 05-Jan-2020 Y 2
3524 06-Jan-2020 Y 3
3524 07-Jan-2020 N 0
3524 08-Jan-2020 N 0
3524 09-Jan-2020 Y 4
3524 10-Jan-2020 Y 5
3524 11-Jan-2020 Y 6
3524 12-Jan-2020 Y 7
5347 04-Oct-2020 Y 1
5347 05-Oct-2020 Y 2
5347 06-Oct-2020 N 0
5347 07-Oct-2020 N 0
5347 08-Oct-2020 N 0
5347 09-Oct-2020 Y 4
5347 10-Oct-2020 Y 5
5347 11-Oct-2020 Y 6

If they are absent on particular day i.e. N, then the rank should be 0, else the rank should be given.

I have tried with the below code, but I am unable to achieve the expected output.

CASE 
WHEN [Date] BETWEEN StartDate AND EndDate AND attendance='N' THEN 0
WHEN [Date] BETWEEN StartDate AND EndDate AND attendance='Y'
THEN RANK() OVER (
PARTITION BY studentid,attendance ORDER BY [Date])
ELSE 0
END AS ranking

答案1

得分: 1

以下是已翻译的部分:

你的数据有些混乱,这使得很难准确了解你的意图。

  1. 你的'Expected Output'图像似乎是出勤报告,附加了排名列,所以我从那里开始。
  2. 对我来说,你的假期表格图像('data')似乎是不必要的。如果是假期,学生不会上学,这将在出勤报告中反映出来。
  3. 我不确定如果学生缺席一天会发生什么。排名是否从1重新开始,还是继续从上次的位置计算?

考虑到这一点,我写了以下代码,生成了预期的输出。 Fiddle

SELECT StudentID
     , [Date]
     , Attendance
     , CASE WHEN Attendance = 'N' THEN 0
            ELSE RANK() OVER (PARTITION BY StudentID, Attendance ORDER BY [Date]) 
        END AS Rnk
FROM data
ORDER BY StudentID, [Date]
英文:

Your data is a bit of a mess which makes it difficult to figure out exactly what you're trying to do.

  1. Your 'Expected Output' image seems to be the attendance report with the rank column added so I started with that.
  2. Your holiday table image ('data') seems to me to be unnecessary. If it's a holiday, the students won't show up at school which would be reflected on the attendance report.
  3. I'm not sure what happens if a student misses a day. Does the rank start back over at 1 or do you keep counting from where it was?

With that in mind, I wrote this which produces the Expected Output. Fiddle

  SELECT StudentID
       , [Date]
       , Attendance
       , CASE WHEN Attendance = 'N' THEN 0
              ELSE RANK() OVER (PARTITION BY StudentID, Attendance ORDER BY [Date]) 
          END AS Rnk
    FROM data
ORDER BY StudentID, [Date]

huangapple
  • 本文由 发表于 2023年4月6日 23:58:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951509.html
匿名

发表评论

匿名网友

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

确定