It is currently Thu Feb 20, 2020 11:59 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: SQLite query to get last row for every id
PostPosted: Thu Jan 23, 2020 7:34 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Mar 27, 2009 9:41 am
Posts: 748
Location: Athens, Greece
I have a SQLite db with a table containing the values:
Code:
id                  date
1                   2020-01-12
1                   2020-01-13
1                   2020-01-18
2                   2020-01-13
2                   2020-01-16
2                   2020-01-21
and I want to get the latest date for every id. I want to get these values:
Code:
id                  date
1                   2020-01-18
2                   2020-01-21
I tried to use MAX() with
Code:
SELECT id, MAX(date) FROM Table ORDER BY id ASC

but it only returnes 2 | 2020-01-21. What is the right query? Any help would be appreciated.


Top
 Profile  
Reply with quote  
 Post subject: Re: SQLite query to get last row for every id
PostPosted: Thu Jan 23, 2020 8:11 pm 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1204
Location: right here
I tried the examples from here:
https://www.sqlitetutorial.net/sqlite-max/
..comes down to
Code:
SELECT id, MAX(date) FROM tbl GROUP BY id
or
Code:
SELECT id as id1, date FROM tbl WHERE date = (SELECT MAX(date) FROM tbl WHERE id = id1)


Code:
UseSQLiteDatabase()

Procedure CheckDatabaseUpdate(Database, Query$)
  Result = DatabaseUpdate(Database, Query$)
  If Result = 0
    Debug DatabaseError()
  EndIf
 
  ProcedureReturn Result
EndProcedure

; DatabaseFile$ = GetTemporaryDirectory()+"Database.sqlite"
;
; If CreateFile(0, DatabaseFile$)
;   CloseFile(0)
 
  DatabaseFile$ = ":memory:"
  If OpenDatabase(0, DatabaseFile$, "", "")
   
    CheckDatabaseUpdate(0, "CREATE TABLE tbl (id int, date date)")
   
    CheckDatabaseUpdate(0, "INSERT INTO tbl (id, date) VALUES ('1', '2020-01-12')")
    CheckDatabaseUpdate(0, "INSERT INTO tbl (id, date) VALUES ('1', '2020-01-13')")
    CheckDatabaseUpdate(0, "INSERT INTO tbl (id, date) VALUES ('1', '2020-01-18')")
    CheckDatabaseUpdate(0, "INSERT INTO tbl (id, date) VALUES ('2', '2020-01-13')")
    CheckDatabaseUpdate(0, "INSERT INTO tbl (id, date) VALUES ('2', '2020-01-16')")
    CheckDatabaseUpdate(0, "INSERT INTO tbl (id, date) VALUES ('2', '2020-01-21')")
   
    q.s = ""
   
    q + "SELECT id, MAX(date) FROM tbl GROUP BY id"
   
    ;q + "SELECT id as id1, date FROM tbl WHERE date = (SELECT MAX(date) FROM tbl WHERE id = id1)"
   
    If DatabaseQuery(0, q)
     
      While NextDatabaseRow(0)
        Debug GetDatabaseString(0, 0)
        Debug GetDatabaseString(0, 1)
        Debug ""
      Wend
     
      FinishDatabaseQuery(0)
    Else
      Debug DatabaseError()
    EndIf
   
    CloseDatabase(0)
  Else
    Debug "Can't open database !"
  EndIf
; Else
;   Debug "Can't create the database file !"
; EndIf


Top
 Profile  
Reply with quote  
 Post subject: Re: SQLite query to get last row for every id
PostPosted: Fri Jan 24, 2020 7:55 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Mar 27, 2009 9:41 am
Posts: 748
Location: Athens, Greece
All I wanted was a "GROUP BY id"....
Thank you very very much!!!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye