I have an application that makes quite a lot of selects, listing its results and allowing the user edit certain values.
The core of my question is if its possible to improve my queries or not given the following query in SQLite:
"SELECT X.data1, X.data2, count(X.id_X) as Quantity_Itens,
(select count(*) from Table2 where id_X=X.id_X) local_Table2,
(select count(*) from Table3 inner join Table2 on Table3.id_Table2 = Table2.id where Table3.id_X=X.id_X and type=1) Quantity_Type1,
(select count(*) from Table3 inner join Table2 on Table3.id_Table2 = Table2.id where Table3.id_X=X.id_X and type=2) Quantity_Type2,
(select count(*) from Table4 where id_X = X.id_X) Quantity_Other,
(select count(*) from Table2 where id_X = X.id_X and status <10) Total_Data FROM Table1 X where (X.type_item = 2 or X.type_item = 4 or X.type_item = 6 or X.type_item = 8) and X.ative = 1 and id_local != 0 group by X.id_X order by X.Alias1"
I am not sure if using promisses will improve in any way this, as I need all those datas before allowing the user to take control again.
Also, may or may not be relevant:
OS: Android 4+
Frameworks: Ionic1, AngularJS, Cordova
In your query there are 5 subqueries executed for each of the rows of Table1.
Only one of them accesses only once 1 table.
2 of them access the same table twice and 2 of them access 2 joined tables twice.
This means there are multiple scans through the same tables for each of the rows of Table1.
Also you are aggregating inside Table1, with GROUP BY id_X, but you have in the SELECT list 2 columns: data1 and data2, which are not included in the GROUP BY clause. This means that the returned values of these columns are arbitrary.
And what is that column Alias1? There is no such column among the returned columns of the query.
Anyway, I suggest that you aggregate first in each table, or join of tables and then join to Table1.
Like this:
SELECT t1.data1,
t1.data2,
t1.Quantity_Items,
t2.local_Table2,
t3.Quantity_Type1,
t3.Quantity_Type2,
t4.Quantity_Other,
t2.Total_Data
FROM (
-- The values returned for data1, data2 and Alias1 will be arbitrary
SELECT id_X, data1, data2, Alias1, COUNT(*) Quantity_Items
FROM table1
GROUP BY id_X
) t1
INNER JOIN (
SELECT id_X, COUNT(*) local_Table2, SUM(status < 10) Total_Data
FROM Table2
GROUP BY id_X
) t2 ON t2.id_X = t1.id_X
INNER JOIN (
SELECT t3.id_X, SUM(type = 1) Quantity_Type1, SUM(type = 2) Quantity_Type2
FROM Table3 t3 INNER JOIN Table2 t2
ON t3.id_Table2 = t2.id
GROUP BY t3.id_X
) t3 ON t3.id_X = t1.id_X
INNER JOIN (
SELECT id_X, COUNT(*) Quantity_Other
FROM Table4
GROUP BY id_X
) t4 ON t4.id_X = t1.id_X
WHERE t1.type_item IN (2, 4, 6, 8)
AND t1.active = 1 AND t1.id_local <> 0
ORDER BY t1.Alias1
I have 4 tables:
tasks table
(task_id , department_id , task_title , task_description , task_start_date , task_due_date , task_rating , task_is_completed)
employees table
(employee_id , department_id , employee_name , employee_salary , employee_hire_date)
departments table
(department_id , department_name)
employees_tasks join table
(employee_id , task_id)
Each table is an entity in room database.
I want to return 2 extra columns with the (select * from employees)
one is for calculating employee's rating (by getting average of task_rating column in tasks table the tasks must be completed) the other column is to show the number of tasks running for that employee (by getting count of rows in tasks with task_is_completed = 0 )
I don't know which table to join with which table. we managed to make two separate SQL statements that return those 2 columns by using union and left joins but they are pretty ugly and when combining them it doesn't work.
what we have tried
select employees.employee_name , employees.employee_id ,avg(tasks.task_rating) as Ratings from employees , tasks inner join employees_tasks on(employees.employee_id = employees_tasks.employee_id )AND tasks.task_id = employees_tasks.task_id where tasks.task_is_completed = 1 group by (employees.employee_name )
union select employees.employee_name, employees.employee_id, avg(0) as Ratings from employees where employees.employee_id not in (select employees.employee_id from employees , tasks inner join employees_tasks on(employees.employee_id = employees_tasks.employee_id ) AND tasks.task_id = employees_tasks.task_id where tasks.task_is_completed = 1 group by (employees.employee_name ) ) group by employees.employee_id order by employees.employee_id ;
select employees.employee_name , employees.employee_id ,count(tasks.task_title) as tasks_Running from employees , tasks inner join employees_tasks on(employees.employee_id = employees_tasks.employee_id )AND tasks.task_id = employees_tasks.task_id where tasks.task_is_completed = 0 group by (employees.employee_name )
union select employees.employee_name , employees.employee_id ,0 as tasks_Running from employees where (employees.employee_id not in (select employees.employee_id from employees , tasks inner join employees_tasks on(employees.employee_id = employees_tasks.employee_id )AND tasks.task_id = employees_tasks.task_id where tasks.task_is_completed = 0 group by (employees.employee_name )))group by (employees.employee_name) order by employees.employee_id ;
We want the output to be like this
(employee_id , department_id , employee_name , employee_salary , employee_hire_date , ratings , numTasksRunning)
I believe the following may suit :-
WITH
-- Common Table Expression 1 - Average of Completed Tasks per employee
employee_completedtask_info AS (
SELECT employees.employee_id,avg(tasks.task_rating) AS atr
FROM employees_tasks
JOIN tasks ON employees_tasks.task_id = tasks.task_id
JOIN employees ON employees_tasks.employee_id = employees.employee_id
WHERE tasks.task_is_completed > 0
GROUP BY employees.employee_id
),
-- Common Table Expression 2 - Incompleted Taks per employee
employee_notcompleted_info AS (
SELECT employees.employee_id,count() AS itc
FROM employees_tasks
JOIN tasks ON employees_tasks.task_id = tasks.task_id
JOIN employees ON employees_tasks.employee_id = employees.employee_id
WHERE tasks.task_is_completed = 0
GROUP BY employees.employee_id
),
-- Common Table Expression 3 - Total Tasks per Employee
employee_total_tasks AS (
SELECT employees.employee_id,count() AS ttc
FROM employees_tasks
JOIN tasks ON employees_tasks.task_id = tasks.task_id
JOIN employees ON employees_tasks.employee_id = employees.employee_id
GROUP BY employees.employee_id
)
SELECT employees.employee_name,
CASE WHEN atr IS NOT NULL THEN atr ELSE 0 END AS average_completed_task_rating,
CASE WHEN itc IS NOT NULL THEN itc ELSE 0 END AS incomplete_task_count,
CASE WHEN ttc IS NOT NULL THEN ttc ELSE 0 END AS total_task_count
FROM employees
LEFT JOIN employee_completedtask_info ON employees.employee_id = employee_completedtask_info.employee_id
LEFT JOIN employee_notcompleted_info ON employees.employee_id = employee_notcompleted_info.employee_id
LEFT JOIN employee_total_tasks ON employees.employee_id = employee_total_tasks.employee_id
;
based upon data generated as per the following :-
DROP TABLE IF EXISTS employees;
CREATE TABLE IF NOT EXISTS employees (employee_id INTEGER PRIMARY KEY, department_id INTEGER, employee_name TEXT, employee_salary REAL, employee_hire_date TEXT);
DROP TABLE IF EXISTS departments;
CREATE TABLE IF NOT EXISTS departments (department_id INTEGER PRIMARY KEY, department_name TEXT);
DROP TABLE IF EXISTS employees_tasks;
CREATE TABLE IF NOT EXISTS employees_tasks (employee_id INTEGER, task_id INTEGER, PRIMARY KEY(employee_id, task_id));
INSERT INTO departments VALUES
(null,'Maths'),(null,'English'),(null,'Craft')
;
INSERT INTO employees VALUES
(null,1,'Fred',55000,'2000-01-02'),
(null,2,'Mary',62000,'1996-03-20'),
(null,3,'Tom',52000,'2004-10-11'),
(null,3,'Susan',72000,'1999-06-14'),
(null,2,'Bert',66000,'2000-10-15'),
(null,1,'Jane',70000,'1992-04-02')
;
INSERT INTO tasks VALUES
(null,3,'Task 001 - Craft','Do the Craft thinggy','2018-01-01','2018-08-19',10,0),
(null,1,'Task 002 - Maths','Do the Maths thinggy','2018-03-14','2019-03-13',20,0),
(null,2,'Task 003 - English','Do the English thinggy','2018-02-14','2018-09-14',8,0),
(null,3,'Task 004 - Craft','Do the Craft job','2018-01-01','2018-08-19',10,1),
(null,1,'Task 005 - Maths','Do the Maths job','2018-03-14','2019-03-13',20,1),
(null,2,'Task 006 - English','Do the English job','2018-02-14','2018-09-14',8,1),
(null,3,'Task 007 - Craft','Craft thinggy','2018-03-03','2018-11-21',10,0),
(null,1,'Task 008 - Maths','Maths thinggy','2018-03-14','2019-03-13',20,0),
(null,2,'Task 009 - English','English thinggy','2018-02-14','2018-09-14',8,0)
;
INSERT INTO employees_tasks VALUES
(1,2),(1,5),(1,8),(1,6),
(2,2),
(3,1),(3,4),(3,7)
;
This results in :-
Note This converts null entries to 0's (i.e. in the above there are no tasks for Susan, Bert and Jane so nulls for their task counts/averages, which complicates matters a little hence the CASE WHEN ... THEN ... ELSE .... END AS clauses).
Note I've included the total tasks counts as this may be useful/wanted (the third CTE extracts this info)
I have a table with below rows.
rowid type value
1 A 13
2 A 14
3 B 12
4 B 15
5 C 17
6 C 16
I want to get the bigest value of type A first, it's rowid=2, then others in order by value DESC, the rowids are 56413
How to implement it sqlite? Thanks a lot!
If you wanted to do this only in the order by:
order by (case when type = 'A' and
value = (select max(t2.value) from t t2 where t2.type = 'A')
then 1 else 2
end),
value desc;
Or:
select t.*
from t cross join
(select max(value) as maxvalue from t where type = 'A'
) ta
order by (case when t.type = 'A' and t.value = ta.maxvalue then 1 else 2 end),
value desc;
Simply use the UNION clause to join 2 queries in one.
SELECT Type, Value FROM YourTableName WHERE rowID = 2
UNION
SELECT Type, Value FROM YourTableName WHERE rowID != 2 ORDER BY value DESC
I've stucked with this query:
SELECT * FROM (
SELECT tab._id t_id, tab.name name, pics.pic1 pic FROM mushrooms tab
JOIN mushrooms_pics pics ON t_id = pics.id WHERE t_id IN
(SELECT item_id FROM coordinate WHERE type = "mushrooms") UNION
SELECT tab._id t_id, tab.name name, pics.pic1 pic FROM berries tab
JOIN berries_pics pics ON t_id = pics.id WHERE t_id IN
(SELECT item_id from coordinate WHERE type = "berries") UNION
SELECT tab._id t_id, tab.name name, pics.pic1 pic FROM herbs tab
JOIN herbs_pics pics ON t_id = pics.id WHERE t_id IN
(SELECT item_id from coordinate WHERE type = "herbs")
)
LEFT JOIN coordinate c ON t_id = c.item_id WHERE t_id IN
(SELECT item_id from coordinate)
The query works fine in "DB Browser for SQLite", but in my app i get an error, here's the error in LOGCAT:
android.database.sqlite.SQLiteException: no such column: herbs (code 1): , while compiling: SELECT name FROM( SELECT tab._id t_id, tab.name name, pics.pic1 pic FROM mushrooms tab JOIN mushrooms_pics pics ON t_id = pics.id WHERE t_id in (SELECT item_id from coordinate WHERE type = mushrooms) UNION SELECT tab._id t_id, tab.name name, pics.pic1 pic FROM berries tab JOIN berries_pics pics ON t_id = pics.id WHERE t_id in (SELECT item_id from coordinate WHERE type = berries) UNION SELECT tab._id t_id, tab.name name, pics.pic1 pic FROM herbs tab JOIN herbs_pics pics ON t_id = pics.id WHERE t_id in (SELECT item_id from coordinate WHERE type = herbs) ) LEFT JOIN coordinate c on t_id = c.item_id WHERE t_id in (SELECT item_id from coordinate)
Are there any ideas how can i fix this?
Thanks ;)
In SQL, strings use single quotes.
Double quotes are used to escape table or column names.
(SQLite allows both in either context, but this does not help in an ambiguous context.)
I need some help with the SUM feature in android app. I have a table that looks something like the following :
I have need to SUM Quantities between last two records Notes and last one record with Note. I need to sum Quantity of rows 31,32 and 33. It would return 90. I've tried
SELECT Sum(QUANTITY) FROM fuel_table WHERE NOTE!='' ORDER BY ID DESC
but it returns SUM of all quantities with note.
I am inclined to phrase the question as: sum the quantity from all rows that have one note "ahead" of them. This suggests:
select sum(quantity)
from (select ft.*,
(select count(*)
from fuel_table ft2
where ft2.note = 'Yes' and ft2.id >= ft.id
) as numNotesAhead
from fuel_table ft
) ft
where numNotesAhead = 1;
WITH max_id_with_note AS
(
SELECT MAX(ID) AS max_id
FROM YourTable
WHERE IFNULL(note, '') <> ''
)
, previous_max_id_with_note AS
(
SELECT max(ID) as max_id
FROM YourTable
WHERE IFNULL(note, '') <> ''
AND ID < (SELECT max_id FROM max_id_with_note)
)
SELECT SUM(Quantity)
FROM YourTable
WHERE (SELECT max_id FROM previous_max_id_with_note)
< ID and ID <=
(SELECT max_id FROM max_id_with_note)
Example at SQL Fiddle.
First select few ROW and from this selection query SUM(). In your case it looks like this:
Select SUM(t1.QUANTITY) FROM (SELECT QUANTITY from fuel_table WHERE NOTE!='' ORDER BY ID limit 2) as t1
Change your query as
SELECT Sum(QUANTITY) FROM fuel_table ORDER BY ID DESC LIMIT 3
I created table like you have and test. Andomar had good idea but made few mistakes!
WITH max_id_with_note AS
(
SELECT MAX(ID) AS max_id
FROM fuel_table
WHERE Note <> ''
)
, previous_max_id_with_note AS
(
SELECT max(ID) as max_id
FROM fuel_table
WHERE Note <> ''
AND ID < (SELECT max_id FROM max_id_with_note)
)
SELECT SUM(Quantity)
FROM fuel_table
WHERE (SELECT max_id FROM previous_max_id_with_note)
< id and id <= (SELECT max_id FROM max_id_with_note)
Use sub query to get your QUANTITY in deceasing order by ID and LIMIT 3 as you want last 3 row and put SUM() to the result quantity...
SELECT SUM(QUANTITY) FROM (SELECT QUANTITY FROM fuel_table ORDER BY ID DESC LIMIT 3);