Skip to content

Instantly share code, notes, and snippets.

@s1037989
Created December 4, 2020 10:54
Show Gist options
  • Save s1037989/07fba641fdfc1f82f79bb1c8350b1f39 to your computer and use it in GitHub Desktop.
Save s1037989/07fba641fdfc1f82f79bb1c8350b1f39 to your computer and use it in GitHub Desktop.
sqlite> CREATE TABLE widget1 ( id INTEGER PRIMARY KEY AUTOINCREMENT, t TIMESTAMP DEFAULT (DATETIME('2020-12-04 04:22:00')), width int);
sqlite> CREATE TABLE widget2 ( id INTEGER PRIMARY KEY AUTOINCREMENT, t TIMESTAMP DEFAULT (DATETIME('2020-12-04 04:22:00')), width int);
sqlite> insert into widget1 (width) values (74);
sqlite> insert into widget1 (width) values (1345);
sqlite> insert into widget1 (width) values (445);
sqlite> insert into widget1 (width) values (6);
sqlite> insert into widget2 (width) values (9999);
sqlite> insert into widget2 (width) values (1);
sqlite> insert into widget2 (width) values (12);
sqlite> insert into widget2 (width) values (4563);
sqlite> select * from widget1;
id|t|width
1|2020-12-04 04:22:00|74
2|2020-12-04 04:22:00|1345
3|2020-12-04 04:22:00|445
4|2020-12-04 04:22:00|6
sqlite> select * from widget2;
id|t|width
1|2020-12-04 04:22:00|9999
2|2020-12-04 04:22:00|1
3|2020-12-04 04:22:00|12
4|2020-12-04 04:22:00|4563
sqlite> create temp table w2 as select (select count(0) from widget2 a where a.width <= b.width) as 'row',* from widget2 b order by width;
sqlite> create temp table w1 as select (select count(0) from widget1 a where a.width <= b.width) as 'row',* from widget1 b order by width;
sqlite> select * from w1;
row|id|t|width
1|4|2020-12-04 04:22:00|6
2|1|2020-12-04 04:22:00|74
3|3|2020-12-04 04:22:00|445
4|2|2020-12-04 04:22:00|1345
sqlite> select * from w2;
row|id|t|width
1|2|2020-12-04 04:22:00|1
2|3|2020-12-04 04:22:00|12
3|4|2020-12-04 04:22:00|4563
4|1|2020-12-04 04:22:00|9999
sqlite> select * from w1 left join w2 using (row);
row|id|t|width|id|t|width
1|4|2020-12-04 04:22:00|6|2|2020-12-04 04:22:00|1
2|1|2020-12-04 04:22:00|74|3|2020-12-04 04:22:00|12
3|3|2020-12-04 04:22:00|445|4|2020-12-04 04:22:00|4563
4|2|2020-12-04 04:22:00|1345|1|2020-12-04 04:22:00|9999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment