diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/JoinAddressQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/JoinAddressQueries.java index f290497b6..1775f6302 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/JoinAddressQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/JoinAddressQueries.java @@ -62,21 +62,24 @@ public class JoinAddressQueries { } public static Query> latestJoinAddresses(ServerUUID serverUUID) { - String selectLatestJoinAddresses = SELECT + + String selectLatestSessionStarts = SELECT + SessionsTable.USER_ID + ",MAX(" + SessionsTable.SESSION_START + ") as max_start" + + FROM + SessionsTable.TABLE_NAME + " max_s" + + WHERE + "max_s." + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + + GROUP_BY + SessionsTable.USER_ID; + String selectLatestJoinAddressIds = SELECT + SessionsTable.JOIN_ADDRESS_ID + + FROM + SessionsTable.TABLE_NAME + " s" + + INNER_JOIN + "(" + selectLatestSessionStarts + ") q1 on q1." + SessionsTable.USER_ID + "=s." + SessionsTable.USER_ID + + AND + "q1.max_start=s." + SessionsTable.SESSION_START; + + String selectJoinAddressCounts = SELECT + "COUNT(1) as total," + JoinAddressTable.JOIN_ADDRESS + - FROM + SessionsTable.TABLE_NAME + " a" + - LEFT_JOIN + SessionsTable.TABLE_NAME + " b on a." + SessionsTable.ID + " db.queryMap(selectLatestJoinAddresses, JoinAddressQueries::extractJoinAddressCounts, TreeMap::new, serverUUID); + return db -> db.queryMap(selectJoinAddressCounts, JoinAddressQueries::extractJoinAddressCounts, TreeMap::new, serverUUID); } public static QueryStatement> allJoinAddresses() {