SQLite query to join on a range of dates?

后端 未结 3 394
别跟我提以往
别跟我提以往 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条回答
  •  [愿得一人]
    2021-01-25 08:30

    Create a calendar table and join onto that:

    Something like this will do:

    create table dates (id integer primary key);
    insert into dates default values;
    insert into dates default values;
    insert into dates select null from dates d1, dates d2, dates d3 , dates d4;
    insert into dates select null from dates d1, dates d2, dates d3 , dates d4;
    alter table dates add date datetime;
    update dates set date=date('2000-01-01',(-1+id)||' day');
    

    That will cover you till 2287-05-20 Adding an index to the date column in the dates table won't hurt. My sqlite is little rusty, so I suggest you consult the manual on that.

    Edit: Code for the index:

    create unique index ux_dates_date on dates (date);
    

提交回复
热议问题