Shantanu Oak ([info]shantanuo) wrote,
@ 2009-06-13 16:54:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Current mood: hopeful
Entry tags:sqlite

SQLite Example file
Here is an example how does SQLite handle datetime function:

create table events (id INTEGER PRIMARY KEY AUTOINCREMENT, name, kind, start, end);

insert into events values (null, 'tom', 'hour', datetime('now', '+1 day','start of day', '+11 hours'), datetime('now', '+1 day','start of day', '+12 hours'));
insert into events values (null, 'tom', 'hour', datetime('now', '+1 day','start of day', '+9 hours'), datetime('now', '+1 day','start of day', '+10 hours'));
insert into events values (null, 'joe', 'hour', datetime('now', '+1 day','start of day', '+9 hours'), datetime('now', '+1 day','start of day', '+10 hours'));
insert into events values (null, 'tom', 'day', datetime('now', '+1 day','start of day'), datetime('now', '+1 day','start of day', '+1 day'));

select *, (strftime('%s', end) - strftime('%s', start)) as length from events;

select *, min((strftime('%s', end) - strftime('%s', start))) as length from events where datetime('now', '+1 day','start of day', '+9 hours','+30 minutes') and end > datetime('now', '+1 day','start of day', '+9 hours','+30 minutes') group by name;




(1 comment) - (Post a new comment)

Correct Query
[info]shantanuo
2009-06-13 11:35 am UTC (link)

select * from events e1
where id in (
select id from events e2 join
(select datetime('now', '+1 day','start of day', '+9 hours','+30
minutes') x)
where e1.name = e2.name and start < x and end > x
order by (julianday(end) - julianday(start)) limit 1
);

The variant below is more complicated, but may run faster:

select * from events e1
where id in (
select (
select id from events e2
where e2.name = names.name and start < x and end > x
order by (julianday(end) - julianday(start)) limit 1
)
from (select distinct name from events) names join
(select datetime('now', '+1 day','start of day', '+9 hours','+30
minutes') x)
);

(Reply to this)


(1 comment) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…