0

I have a table packs with four columns INT ID (Primary Key), Char Code, DATETIME Approved, and INT Score. For each unique code, I want to sort the entries associated with it by score and then transfer the ID with the highest score with a non-NULL DATETIME to a second table airports. The airports table is already built with Char Code as a the primary key, with a currently NULL INT column RecommendedPack. The ID associated with the highest score for that code (if applicable) should fill that column. I've only done pretty basic SQL before, so I'm not really sure how to begin with this problem. I am using MySQL 5.7 for reference.

Sample Data:

ID   Code   Approved  Score
---------------------------
1    KLAX   Valid DT  1
2    KBOS   Valid DT  0
3    KLAX   Valid DT  -1
4    KSFO   Valid DT  0
5    KSFO   NULL      5
6    KSMO   Valid DT  1

Expected result:

    Code   ...  RecommendedPack
    ---------------------------
    KBOS        2
    KLAX        1
    KSFO        4
    KSMO        6

The suggested duplicate does not solve my problem because I need to find that max without specifying a threshold.

laminarflow
  • 55
  • 1
  • 10

0 Answers0