Results 1 to 5 of 5

Thread: How to fetch maximum record from my table in mySql database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    How to fetch maximum record from my table in mySql database

    Hi. I've a table purchases which has the following fields:

    Code:
    purchase_id
    vendor_id
    fuel_type_id
    purchase_quantity_liters
    purchase_price_per_liter
    date_of_purchase
    is_deleted
    I want to fetch that latest record which has fuel_type_id = 1.

    Actually, this table gets entry when the fuel (petrol/ diesel) is purchased. So I want to fetch only that record of purchase which has fuel_type_id 1 or 2.

    I've used this query but its not showing the latest record:

    Code:
    SELECT
        MAX(p.purchase_id), Id
        p.purchase_price_per_liter,
        p.fuel_type_id
    FROM
        purchases p
    WHERE
        p.fuel_type_id = 1;
    There are currently only 3 records, 2 records for fuel_type_id 1 and 1 for ID = 2;

    The result this query brings me has the latest ID but not the latest Price?

    Please guide me that what I'm missing?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,936

    Re: How to fetch maximum record from my table in mySql database

    Aggregate functions like Max() only apply to the field they are on, they do not affect the other fields.

    In order to get just one record you could use various options, including:
    Code:
    SELECT
        p.purchase_id Id,
        p.purchase_price_per_liter,
        p.fuel_type_id
    FROM
        purchases p
    WHERE
        p.fuel_type_id = 1;
        AND p.purchase_id = (
            SELECT MAX(purchase_id)
            FROM purchases 
            WHERE p.fuel_type_id = 1
            )
    ;

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,745

    Re: How to fetch maximum record from my table in mySql database

    For MySQL8
    Code:
    WITH
        CT    AS (SELECT *, 
                ROW_NUMBER() OVER(PARTITION BY fuel_type_id ORDER BY date_of_purchase DESC) AS RN 
                FROM purchases)
    SELECT * FROM CT WHERE RN=1
    For MySQL5.7 and older
    Code:
    SELECT purchase_id, vendor_id, fuel_type_id, purchase_quantity_liters, purchase_price_per_liter, date_of_purchase, is_deleted FROM (
      SELECT *, IF(@prev <> fuel_type_id, @rn:=0,@rn), @prev:=fuel_type_id, @rn:=@rn+1 AS rn
      FROM purchases, (SELECT @rn:=0) rn, (SELECT @prev:='') prev
      ORDER BY fuel_type_id ASC, date_of_purchase DESC
    ) t WHERE rn = 1;
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,382

    Re: How to fetch maximum record from my table in mySql database

    Code:
    SELECT * FROM purchases WHERE purchase_id = 
    (SELECT TOP 1 purchase_id FROM purchases WHERE fuel_type_id = 1 ORDER BY date_of_purchase DESC)

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,745

    Re: How to fetch maximum record from my table in mySql database

    Quote Originally Posted by digitalShaman View Post
    Code:
    SELECT * FROM purchases WHERE purchase_id = 
    (SELECT TOP 1 purchase_id FROM purchases WHERE fuel_type_id = 1 ORDER BY date_of_purchase DESC)
    That’s for MS SQL
    OP states MySQL
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width