Search code examples
androidsqlsqliteandroid-sql

SQL Query for timestamp


I had following Table

CREATE TABLE Customer
    ( `Name` varchar(7), `Address` varchar(55), `City` varchar(15),`Contact` int,`timestamp` int)
;

INSERT INTO Customer
    (`Name`,`Address`, `City`, `Contact`,`timestamp`)
VALUES
    ('Jack','New City','LA',79878458,456125),
    ('Joseph','New Lane23','LA',87458458,794865),
   ('Rosy','Old City','Paris',79878458,215125),
   ('Maria','New City','LA',79878458,699125),
   ('Jack','New City','LA',79878458,456125),
   ('Rosy','Old City','Paris',79878458,845125),
   ('Jack','New Main Street','New York',79878458,555525),
   ('Joseph','Near Bank','SAn Francisco',79878458,984521)

;

I want to get all customer record with highest timestamp without duplication.


Solution

  • I want to get all customer record with highest timestamp without duplication.

    Use DISTINCT operator and ORDER BY clause like

    select distinct `Name`,`Address`, `City`, `Contact`,`timestamp`
    from customer
    order by `timestamp` desc;
    

    In that case you can use JOIN query like

    select t1.*
    from customer t1 join
    (select Name, max(`timestamp`) as maxstamp
     from customer
     group by Name) xx 
     on t1.Name = xx.Name
     and t1.`timestamp` = xx.maxstamp;