I have two tables I’m trying to join and get a unique value in one column based on the max value of another column plus an identifier in another table. This is my structure.
Partial Table 1 (reward_webuid):
╔════╦═════════╦═════════╦══════════╦═════════════════════╦═════════╗
║ ID ║ WEB_UID ║ ITEM_ID ║ PLAYERID ║ TIMESTAMP ║ EXPIRED ║
╠════╬═════════╬═════════╬══════════╬═════════════════════╬═════════╣
║ 8 ║ 1209520 ║ 73410 ║ 1926406 ║ 2013-01-05 04:47:06 ║ 0 ║
║ 9 ║ 1209520 ║ 73425 ║ 1926406 ║ 2013-01-02 09:40:27 ║ 1 ║
║ 10 ║ 1209520 ║ 73424 ║ 1926406 ║ 2013-01-05 09:40:05 ║ 0 ║
║ 11 ║ 1209520 ║ 73425 ║ 1926406 ║ 2013-01-05 09:40:27 ║ 0 ║
║ 12 ║ 1209521 ║ 73424 ║ 21555896 ║ 2013-01-05 01:33:20 ║ 0 ║
║ 13 ║ 1209521 ║ 75000 ║ 5599854 ║ 2013-01-05 01:35:09 ║ 1 ║
╚════╩═════════╩═════════╩══════════╩═════════════════════╩═════════╝
Partial Table 2 (rewards):
╔═════════╦═════════════════╦══════════════╦═════════════╗
║ ITEM_ID ║ CUST_LOADOUT_ID ║ REWARD_LEVEL ║ EXPIRY_TIME ║
╠═════════╬═════════════════╬══════════════╬═════════════╣
║ 73410 ║ 1 ║ 5 ║ 0 ║
║ 73424 ║ 2 ║ 10 ║ 0 ║
║ 73425 ║ 3 ║ 15 ║ 30 ║
║ 75000 ║ 4 ║ 50 ║ 60 ║
╚═════════╩═════════════════╩══════════════╩═════════════╝
Sample return of what I would like to get (cust_loadout_id and playerID for the maximum value of reward level based on item_id in table 1 that hasn’t expired yet) there should only be one value per playerID
╔══════════╦═════════════════╗
║ PLAYERID ║ CUST_LOADOUT_ID ║
╠══════════╬═════════════════╣
║ 1926406 ║ 3 ║
║ 21555896 ║ 2 ║
╚══════════╩═════════════════╝
Would be grateful if anyone could help with this. I searched and most queries related to the same table or 3 or more tables. The best I’ve managed is to return data I needed but it wasn’t ordered correctly
1 Answer