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

KitaYama

Well-known member
Local time
Today, 20:49
Joined
Jan 6, 2022
Messages
2,279
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;
 
A subquery with TOP 1 can work well for your scenario. See example below.

SQL:
SELECT
    p.PricesProductFK,
    p.PricesOrderedFromFK,
    p.PricesQuantity,
    p.PricesRegisteredOn AS RegisteredOn,
    p.PricesRegisteredBy,
    p.PricesUnitPrice
FROM
    tblProductPrices AS p
WHERE
    p.PricePK IN (
        SELECT TOP 1 p2.PricePK
        FROM tblProductPrices AS p2
        WHERE
            p2.PricesProductFK = p.PricesProductFK
            AND p2.PricesOrderedFromFK = p.PricesOrderedFromFK
            AND p2.PricesQuantity = p.PricesQuantity
            AND p2.PricesRegisteredBy = p.PricesRegisteredBy
        ORDER BY  p2.PricesRegisteredOn DESC, p2.PricesUnitPrice DESC
    )
    AND p.PricesProductFK = 34086;
 
The subquery in post #2 was incorrect. It gives two records for PricesQuantity = 10 & a total of 8 records
The version suggested by @DaveMD is both much simpler and appears to be correct with only 7 records
 
The subquery in post #2 was incorrect. It gives two records for PricesQuantity = 10 & a total of 8 records
The version suggested by @DaveMD is both much simpler and appears to be correct with only 7 records
The result data shown in my first post, was a copy and paste from the data sheet view of sql from #2. It shows only 7 records and it seems the result is correct.
But I may be wrong though. I did lot of testing and may have pasted the wrong sql.
I'm away from my PC and will try to start a remote control to :
Recheck the #2 sql and @Dave E's suggestion.

Thanks to both for your help.
I really appreciate it.
 
The subquery in post #2 was incorrect. It gives two records for PricesQuantity = 10 & a total of 8 records
The version suggested by @DaveMD is both much simpler and appears to be correct with only 7 records
I just rechecked.
The sql of #2 seems to show the correct result.
Thanks.
 
A subquery with TOP 1 can work well for your scenario. See example below.

SQL:
SELECT
    p.PricesProductFK,
    p.PricesOrderedFromFK,
    p.PricesQuantity,
    p.PricesRegisteredOn AS RegisteredOn,
    p.PricesRegisteredBy,
    p.PricesUnitPrice
FROM
    tblProductPrices AS p
WHERE
    p.PricePK IN (
        SELECT TOP 1 p2.PricePK
        FROM tblProductPrices AS p2
        WHERE
            p2.PricesProductFK = p.PricesProductFK
            AND p2.PricesOrderedFromFK = p.PricesOrderedFromFK
            AND p2.PricesQuantity = p.PricesQuantity
            AND p2.PricesRegisteredBy = p.PricesRegisteredBy
        ORDER BY  p2.PricesRegisteredOn DESC, p2.PricesUnitPrice DESC
    )
    AND p.PricesProductFK = 34086;
I just tested your version. The result is correct, But it seems yo be very slower than the joined sql in #2.
The #2 sql is instantaneous, but running your sql, takes around 1 to 2 seconds. First there's a blink then the result shows.

If I chnge the where clause to
AND p.PricesProductFK IN (34086, 34087, 34089, 34090, 34010);
it takes much longer time.
BE is sql server on a active directory. Table is around 10K, but is expected to grow to half a million records.

Thanks for your time.
 

Users who are viewing this thread

Back
Top Bottom