This SQL problem is the MAX() amount of frustration I can handle right now. (2 Viewers)

KitaYama

Well-known member
Local time
Today, 18:27
Joined
Jan 6, 2022
Messages
2,275
I'm sorry for the length of the question. I don't know how I can explain it shorter.
This is a working sql:
SQL:
SELECT
    PricesOrderedFromFK,
    PricesQuantity,
    Max(PricesRegisteredOn) As RegisteredOn,
    PricesRegisteredBy
FROM
    tblProductPrices
WHERE
    PricesProductFK = 34086
Group BY
    PricesOrderedFromFK,
    PricesQuantity,
    PricesRegisteredBy
Order BY
    PricesQuantity,
    Max(PricesRegisteredOn);

I want to add a field (PricesUnitPrice) to the query from the same record that produced Max(PricesRegisteredOn) As RegisteredOn.
I can not add PricesUnitPrice as a group, because it causes duplicates in PricesQuantity.
I think I need a sub query, but alas, that's out of my knowledge.

The data in table is :

PricePKPricesProductFKPricesOrderedFromFKPricesRegisteredOnPricesQuantityPricesUnitPricePricesRegisteredBy
612340861592016/03/011699.00126
613340861592016/03/012583.00126
614340861592016/03/016556.00126
615340861592016/03/0110530.00126
1743340861592016/03/0110625.00126
616340861592024/11/0521503.00126
1178340861592016/03/0121594.00126
2417340861592025/12/2021802.00126
617340861592016/03/0151492.00126
619340861592023/10/1751581.00126
618340861592016/03/01101489.00126

Expected query result is : (This is the result of above sql + The additional field "PricesUnitPrice")
(red records in above table)

PricesOrderedFromFKPricesQuantityRegisteredOnPricesRegisteredByPricesUnitPrice
5912016/03/01126699.00
5922016/03/01126583.00
5962016/03/01126556.00
59102024/11/05126625.00
59212025/12/20126802.00
59512023/10/17126581.00
591012016/03/01126489.00

Expected result rule is:
1- No duplicates in PricesQuantity (Should be grouped and sorted ASC)
2- The latest date of PricesQuantity (in each group of rule one : PricesQuantity field).
3- The PricesUnitPrice field for the result record in rule 1 and 2.


Thanks for your time and help.
 
Last edited:
After several trials and errors, the following seems to work.
But honestly, I'm not sure if it can be relied on. There seems to be too many joins:

I appreciate if you can correct this one, or suggest something to be used instead.
Thanks.

SQL:
SELECT
    q.PricesOrderedFromFK,
    q.PricesQuantity,
    q.RegisteredOn,
    q.PricesRegisteredBy,
    p.PricesUnitPrice
FROM
    (
        SELECT
            PricesProductFK,
            PricesOrderedFromFK,
            PricesQuantity,
            PricesRegisteredBy,
            Max(PricesRegisteredOn) AS RegisteredOn
        FROM
            tblProductPrices
        WHERE
            PricesProductFK = 34086
        GROUP BY
            PricesProductFK,
            PricesOrderedFromFK,
            PricesQuantity,
            PricesRegisteredBy
    ) AS q
    INNER JOIN tblProductPrices AS p ON p.PricesProductFK = q.PricesProductFK
    AND p.PricesOrderedFromFK = q.PricesOrderedFromFK
    AND p.PricesQuantity = q.PricesQuantity
    AND p.PricesRegisteredBy = q.PricesRegisteredBy
    AND p.PricesRegisteredOn = q.RegisteredOn
ORDER BY
    q.PricesQuantity,
    q.RegisteredOn;
 

Users who are viewing this thread

  • Back
    Top Bottom