Re: [sqlite] Doing math in sqlite

2018-12-21 Thread Jungle Boogie
On Thu 20 Dec 2018 4:21 PM, Jungle Boogie wrote: > Hi All, > > This is more of a how do I do this in sql question. I apologize in advance > for a simple question, but I need to learn somehow, so any pointers are > appreciated. Thank you all for the helpful replies and education on doing math w

Re: [sqlite] Doing math in sqlite

2018-12-20 Thread Dingyuan Wang
You can use recently supported window function (portable): SELECT * FROM ( SELECT car, lag(date) OVER w last_date, date, od_reading - (lag(od_reading) OVER w) diff FROM mileage WINDOW w AS (PARTITION BY car ORDER BY date) ) q WHERE diff IS NOT NULL; 2018/12/21 11:48, Jungle Boogie: > On

Re: [sqlite] Doing math in sqlite

2018-12-20 Thread Barry
The following will give you information pretty much as your example: car, start date, end date, distance. SELECT m1.Car, m2.Date AS StartDate, m1.Date AS endDate, m1.od_reading - m2.od_reading AS distance FROM mileage AS m1 JOIN mileage AS m2 ON m2.rowid = (SELECT rowid FROM mileage m3 WHERE m3.Da

Re: [sqlite] Doing math in sqlite

2018-12-20 Thread Keith Medcalf
On Thursday, 20 December, 2018 17:32, Jens Alfke wrote: >> On Dec 20, 2018, at 4:21 PM, Jungle Boogie wrote: >> select od_reading from mileage where car='foo' limit 1 >> select od_reading from mileage where car='bar' order by od_reading >> desc limit 1 >Note: the first query should use “order

Re: [sqlite] Doing math in sqlite

2018-12-20 Thread Jungle Boogie
On Thu 20 Dec 2018 6:26 PM, Barry Smith wrote: > > > > On 20 Dec 2018, at 4:21 pm, Jungle Boogie wrote: > > > > Hi All, > > > > Some sample data: > > 2018/04/15,foo,170644 > > 2018/04/15,bar.69625 > > 2018/04/22,foo,170821 > > 2018/04/22,bar,69914 > > 2018/04/29,foo,171006 > > 2018/04/29,bar,

Re: [sqlite] Doing math in sqlite

2018-12-20 Thread Barry Smith
> On 20 Dec 2018, at 4:21 pm, Jungle Boogie wrote: > > Hi All, > > This is more of a how do I do this in sql question. I apologize in advance > for a simple question, but I need to learn somehow, so any pointers are > appreciated. > > My very simple schema: > > CREATE TABLE mileage ( > date

Re: [sqlite] Doing math in sqlite

2018-12-20 Thread Shevek
For the cost of a single table scan, you may be better with: select max(foo) - min(foo) where etc. S. -- Anyone using floating point for financial computations needs their head examined. On 12/20/18 4:32 PM, Jens Alfke wrote: On Dec 20, 2018, at 4:21 PM, Jungle Boogie wrote: select od_

Re: [sqlite] Doing math in sqlite

2018-12-20 Thread Jens Alfke
> On Dec 20, 2018, at 4:21 PM, Jungle Boogie wrote: > > select od_reading from mileage where car='foo' limit 1 > select od_reading from mileage where car='bar' order by od_reading desc limit > 1 Note: the first query should use “order by od_reading”, otherwise the order is undefined. A clea