简体   繁体   中英

SQLite query for retrieving data in order by for string

TableMain

Task_Id  Task       Time     Date         Repetition    Snooze_Time
1        Meeting    22:59    23-3-2020    2             7
2        Meeting 2  20:40    23-3-2020    1             5 

I need result like this: Where the Time is least and Date = 23-3-2020 Note: Time is stored as String(as you can see it has (:)colon) Result should contain only 1 row.(Suppose there is no same time in the table)

Task_Id  Task       Time     Date         Repetition    Snooze_Time
2        Meeting 2  20:40    23-3-2020    1             5

I want to know that is query exist to retrieve data order by or Time or not. If possible tell me the query. If not possible tell me what can I do?

You can use NOT EXISTS:

select t.* from TableMain t
where t.date = '23-3-2020'
and not exists (
  select 1 from TableMain
  where date = t.date and time < t.time
)

or with row_number() window function:

select t.Task_Id, t.Task, t.Time, t.Date, t.Repetition, t.Snooze_Time
from (
  select *, row_number() over (order by time) rn
  from TableMain 
  where t.date = '23-3-2020'
) t
where t.rn = 1

or:

select * from TableMain
where date = '23-3-2020'
order by time
limit 1

Note that you should only use the format YYYY-MM-DD for your dates and mm:hh:ss for time, because this is the only format that you can safely work with in SQLite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM