SQLite query to join on a range of dates?

后端 未结 3 428
别跟我提以往
别跟我提以往 2021-01-25 07:55

I am working with SQLite.

Suppose I have a table sales with two columns, date and count, to keep track of how many glasses of lemo

3条回答
  •  萌比男神i
    2021-01-25 08:29

    I think you need something like this:

    SELECT sum(count) / ((strftime('%s','2010-01-10')-strftime('%s','2010-01-04')+86400)/86400)
    FROM sales
    WHERE date IS BETWEEN '2010-01-04' AND '2010-01-10';
    

    The query calculates the exact number of days by taking the seconds between the given dates
    and dividing with 86400 (= 24*3600) secs.

    For example

    SELECT (strftime('%s','2010-03-01')-strftime('%s','2010-01-01') + 86400) / 86400
    

    outputs 60 which is the correct value.

    Check out SQLite's Date And Time Functions.

提交回复
热议问题