sqlite3 - How do I add range to one of my query requirments? -
i have 2 tables. first has series of data 3 columns [z, s, c] call tblbaseline. second table has same structure [z,s,c] call tbldatalog. want find records in tblbaseline match tbldatalog using column s , similar in tbldatalog not identical. can pull matching records based on column s, how additionally filter column c given want record tblbaseline plus or minus 5 of corresponding record in tbldatalog.
this code gives me matching records based on column s , c:
select baseline.count baseline left join data_log baseline.sample=data_log.sample , baseline.count=data_log.count;
this code not work...
select baseline.count baseline left join data_log baseline.sample=data_log.sample , baseline.count>= data_log.count -3 or baseline.count<= data_log.count +3;
here table structure:
create table baseline (id integer primary key, zone integer, sample integer, count integer, sqltimestamp datetime default current_timestamp); create table data_log (id integer primary key, zone integer, sample integer, count integer, sqltimestamp datetime default current_timestamp);
here table data:
sqlite> select * baseline;
1|2|1|186|2015-07-24 16:31:38 2|2|2|183|2015-07-24 16:31:47 3|2|3|170|2015-07-24 16:32:01 4|2|4|165|2015-07-24 16:32:12 5|5|1|115|2015-07-24 16:32:26 7|5|2|113|2015-07-24 16:33:12 8|5|3|111|2015-07-24 16:33:23 9|5|4|109|2015-07-24 16:33:33
sqlite> select * data_log;
1|0|1|115|2015-07-24 16:34:09 2|0|2|113|2015-07-24 16:34:18 3|0|3|111|2015-07-24 16:34:28 4|0|4|109|2015-07-24 16:34:42
the following queries , results:
sqlite> select baseline.count baseline left join data_log baseline.sample=data_log.sample , baseline.count=data_log.count;
115 113 111 109
sqlite> select baseline.count baseline left join data_log baseline.sample=data_log.sample , baseline.count between data_log.count+60 , data_log.count-60;
the query +/- 60 condition should return records 5-9 , additionally record 4 of baseline table.
left join works 1 join condition in on clause.
and shown in documentation, , has higher precedence or, query same as:
select baseline.count baseline left join data_log ( baseline.sample = data_log.sample , baseline.count >= data_log.count - 3) or baseline.count <= data_log.count + 3;
you should insert correct parentheses:
select baseline.count baseline left join data_log on baseline.sample = data_log.sample , (baseline.count >= data_log.count - 3 or baseline.count <= data_log.count + 3 );
or use between:
select baseline.count baseline left join data_log on baseline.sample = data_log.sample , baseline.count between data_log.count - 3 , data_log.count + 3;
Comments
Post a Comment