'How to create correct mysql update query using window function for grouped items

We have a table that you could label as a summary table that we need to update every so often from another table that contains dated sales rows. How do you create a query, along with the index for it, that pulls the latest record from table x to update table summary table y? I was thinking a mysql window function would seem like it would fit here, but not sure how to create it as, nor the index it would need. The index is extremely important, as the product sales table will contain over 100 mil rows. I understand that it may take two different queries to update category group A & B values.

I posted this as well on the DBA stackexchange, I see a lot of mysql questions asked here as well and was not getting any response over there.

CREATE TABLE SalesSummary (
    SalesPersonId INT NOT NULL,
    LastSoldProductInCategoryGroupA INT,
    TotalSoldInCategoryGroupA INT NOT NULL DEFAULT 0,
    LastSoldProductInCategoryGroupB INT,
    TotalSoldInCategoryGroupB INT NOT NULL DEFAULT 0,

    PRIMARY KEY (SalesPersonId)
);

CREATE TABLE ProductSales (
    Id INT NOT NULL AUTO_INCREMENT,
    SalesPersonId INT NOT NULL AUTO_INCREMENT,
    ProductID INT NOT NULL,
    ProdCategory INT NOT NULL,
    Created DATETIME NOT NULL DEFAULT (utc_timestamp()),

    PRIMARY KEY (Id)
);

Expected SalesSummary

SalesPersonId LastSoldProductInCategoryGroupA TotalSoldInCategoryGroupA LastSoldProductInCategoryGroupB TotalSoldInCategoryGroupB
1 300 1 100 2
2 700 1 800 1
3 700 1 NULL 0
4 NULL 0 800 2

Category Groups: A=2, 5; B=1, 4

ProductSales data

Id SalesPersonId ProductID ProdCategory Sold
1 1 100 4 2022-05-04
2 2 700 2 2022-05-04
3 2 800 4 2022-05-03
4 1 500 1 2022-05-03
5 3 700 2 2022-05-02
6 4 800 4 2022-05-02
7 2 200 3 2022-05-02
8 4 500 1 2022-05-01
9 1 300 5 2022-05-01


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source