MAX() and MAX() OVER PARTITION BY produces error 3504 in Teradata Query

后端 未结 5 1344
猫巷女王i
猫巷女王i 2021-02-15 10:05

I am trying to produce a results table with the last completed course date for each course code, as well as the last completed course code overall for each employee. Below is my

5条回答
  •  别那么骄傲
    2021-02-15 10:47

    I think this will work even though this was forever ago.

    SELECT employee_number, Row_Number()  
       OVER (PARTITION BY course_code ORDER BY course_completion_date DESC ) as rownum
    FROM employee_course_completion
    WHERE course_code IN ('M910303', 'M91301R', 'M91301P')
       AND rownum = 1
    

    If you want to get the last Id if the date is the same then you can use this assuming your primary key is Id.

    SELECT employee_number, Row_Number()  
       OVER (PARTITION BY course_code ORDER BY course_completion_date DESC, Id Desc) as rownum    FROM employee_course_completion
    WHERE course_code IN ('M910303', 'M91301R', 'M91301P')
       AND rownum = 1
    

提交回复
热议问题