I am trying to come up with an SQLite query which would retrieve all the row values between two given values (A and B) in the query,upon meeting a condition.
if (value B given is greater than the maximum value of B in the table):
- retrieve all values between A and B
Sample Table: inventory
Prod_name | model | location |
tesla | "5.6.1" | CA
toyota | "4.7.1" | WA
kia | "6.8.1" | MD
tesla | "2.6.2" | CA
chev | "7.8.4" | AZ
Input given : model between ("5.0.0" to "8.2.0")
Output : (telsa,5.6.1,CA),(kia,6.8.1,MD) , (chev,7.8.4,AZ)
Input given : model between ("5.0.0" to "6.9.0")
Output: Query should not run as "7.8.4" > "6.9.0"
i.e ( the max value in the table is greater than the upper limit of input query.
Also to note is the model name is TEXT format. I need help to retrieving
I have tried "CASE" statements of sqlite but was not able to retrieve
multiple columns in the subquery.
select
case
when (select 1000000 * replace(model, '.', 'x') +
1000 * replace(substr(model, instr(model, '.') + 1), '.', 'x') +
replace(model, '.', '000') % 1000 as md from inventory ORDER BY md
DESC LIMIT 1) > (select 1000000 * replace('5.0.0', '.', 'x') +
1000 * replace(substr('5.0.0', instr('5.0.0', '.') + 1), '.', 'x') +
replace('5.0.0', '.', '000') % 1000)
THEN (select model from inventory where
1000000 * replace(model, '.', 'x') +
1000 * replace(substr(model, instr(model, '.') + 1), '.', 'x') +
replace(model, '.', '000') % 1000
between
1000000 * replace('5.0.0' '.', 'x') +
1000 * replace(substr(''5.0.0'', instr('5.0.0', '.') + 1), '.',
'x') +
replace('5.0.0', '.', '000') % 1000
and
1000000 * replace('8.5.0', '.', 'x') +
1000 * replace(substr('8.5.0', instr('8.5.0', '.') + 1), '.', 'x') +
replace('8.5.0', '.', '000') % 1000 )
END from inventory
I believe that the following will do what you want :-
/* Query using model in n.n.n format */
SELECT * FROM inventory
WHERE
((1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000)
BETWEEN
(
SELECT 1000000 * substr('5.0.0',1,instr('5.0.0','.') -1)
+ (1000 * replace(substr('5.0.0',instr('5.0.0','.') + 1),'.','x'))
+ replace('5.0.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
/* MAX COndition */
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
>
(
SELECT MAX(((1000000 * substr(model,1,instr(model,'.')-1))
+ (1000 * replace(substr(model,instr(model,'.') + 1),'.','x'))
+ replace(model,'.','000') % 1000))
FROM inventory
)
ORDER BY
(1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000
;
I am curious to know how this could be used in the current solution.
Or if you have any other approach ?
I would suggest that you are grossly over-complicating matters by using a model that is formatted as n.n.n.
If you were to convert that model to an integer value matters could be greatly simplified.
If you really want to keep the model as n.n.n then perhaps ALTER the table to add a column that stores the model as an integer. e.g. you could, as a one of, use :-
ALTER TABLE inventory ADD COLUMN model_value INTEGER DEFAULT -1;
This adds the column model_value
The ALTER could be followed by a mass UPDATE to then set the values for existing rows e.g. :-
UPDATE inventory SET model_value =
(1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000;
To circumvent needing to change the insert and pre-calculate the model_value, you could add an AFTER INSERT TRIGGER e.g. :-
CREATE TRIGGER IF NOT EXISTS inventory_generate_modelvalue AFTER INSERT ON inventory
BEGIN
UPDATE inventory
SET model_value = (1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000
WHERE model_value < 0 OR model_value IS NULL
;
END;
Note that if you currently use INSERT without specifying the columns, then the insert would have to be adjusted to specify the columns to be used for the insert, OR you could hard code -1 or NULL for the new column.
The query would then be simpler as :-
/* Query using model_value) */
SELECT * FROM inventory
WHERE model_value
BETWEEN
(
SELECT 1000000 * substr('5.0.0',1,instr('5.0.0','.') -1)
+ (1000 * replace(substr('5.0.0',instr('5.0.0','.') + 1),'.','x'))
+ replace('5.0.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
>
(SELECT MAX(model_value) FROM inventory)
ORDER BY model_value
;
If you wanted convert the model value to n.n.n format you could use base this upon :-
SELECT prod_name,
CAST (model_value / 1000000 AS TEXT)
||'.'
|| CAST((model_value % 1000000) / 1000 AS TEXT)
||'.'
||CAST(model_value % 1000 AS TEXT)
AS model,
location
FROM inventory;
Of course if you had a function within your program or used integer values rather than n.n.n then matters would be even simpler.
Testing
The following code was used for testing the above :-
DROP TABLE IF EXISTS inventory;
DROP TRIGGER IF EXISTS inventory_generate_modelvalue;
CREATE TABLE IF NOT EXISTS inventory (prod_name TEXT ,model TEXT,location TEXT);
INSERT INTO inventory VALUES ('tesla','5.6.1','CA'),('toyota','4.7.1','WA'),('kia','6.8.1','MD'),('tesla','2.6.2','CA'),('chev','7.8.4','AZ') ;
/* Add new column for model as an integer value */
ALTER TABLE inventory ADD COLUMN model_value INTEGER DEFAULT -1;
/* Update existing data for new column */
UPDATE inventory SET model_value =
(1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000;
CREATE TRIGGER IF NOT EXISTS inventory_generate_modelvalue AFTER INSERT ON inventory
BEGIN
UPDATE inventory
SET model_value = (1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000
WHERE model_value < 0 OR model_value IS NULL
;
END;
-- INSERT INTO inventory VALUES('my new model','5.0.1','AA',null),('another','0.999.999','ZZ',-1);
SELECT * FROM inventory;
/* Query using model in n.n.n format */
SELECT * FROM inventory
WHERE
((1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000)
BETWEEN
(
SELECT 1000000 * substr('5.0.0',1,instr('5.0.0','.') -1)
+ (1000 * replace(substr('5.0.0',instr('5.0.0','.') + 1),'.','x'))
+ replace('5.0.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
/* MAX COndition */
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
>
(
SELECT MAX(((1000000 * substr(model,1,instr(model,'.')-1))
+ (1000 * replace(substr(model,instr(model,'.') + 1),'.','x'))
+ replace(model,'.','000') % 1000))
FROM inventory
)
ORDER BY
(1000000 * substr(model,1,instr(model,'.')-1)) +
(1000 * replace(substr(model,instr(model,'.') + 1),'.','x')) +
replace(model,'.','000') % 1000
;
/* Query using model_value) */
SELECT * FROM inventory
WHERE model_value
BETWEEN
(
SELECT 1000000 * substr('5.0.0',1,instr('5.0.0','.') -1)
+ (1000 * replace(substr('5.0.0',instr('5.0.0','.') + 1),'.','x'))
+ replace('5.0.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
AND
(
SELECT 1000000 * substr('8.5.0',1,instr('8.5.0','.') -1)
+ (1000 * replace(substr('8.5.0',instr('8.5.0','.') + 1),'.','x'))
+ replace('8.5.0','.','000') % 1000
)
>
(SELECT MAX(model_value) FROM inventory)
ORDER BY model_value
;
SELECT prod_name,
CAST (model_value / 1000000 AS TEXT)
||'.'
|| CAST((model_value % 1000000) / 1000 AS TEXT)
||'.'
||CAST(model_value % 1000 AS TEXT)
AS model,
location
FROM inventory;