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'm a new android developer i want get the data from sqlite database from three tables such as ItemMaster,ItemGroupMaster,SalesDetail
ItemMaster
ItemGroupMaster
SalesDetails
After using this query : select sd.salesDate,igm.groupName,im.itemName,im.itemprice from salesdetails sd inner join itemmaster im on sd.itemId=im.itemId inner join itemgroupmaster igm on im.groupId=igm.groupId
Result
Now i want get actual result is look like below image
Please help me
Thanks in advance
Try this. The Qty and NetAmount columns are both aggregates, and NetAmount relies on Qty, so I used a couple of nested queries:
SELECT Y.salesDate
, Y.GroupName
, Y.ItemName
, Y.Qty
, Y.Price
, Y.Price * Y.Qty AS NetAmount
FROM
(SELECT X.salesDate
, X.groupName AS GroupName
, X.itemName AS ItemName
, X.itemprice AS Price
, COUNT(*) AS Qty
FROM
(SELECT sd.salesDate
,igm.groupName
,im.itemName
,im.itemprice
FROM salesdetails sd
INNER JOIN itemmaster im
ON sd.itemId = im.itemId
INNER JOIN itemgroupmaster igm
ON im.groupId = igm.groupId
) X
GROUP BY 1,2,3
) Y
GROUP BY 1,2,3,4,5
ORDER BY 1,2,3,4,5
I want records from multiple tables. My requirement is to get all columns form one table and some interested columns from different table(s).
We can use "select * from table_name" for normal fetch query. But this is not working with join query, I have to write all column name for that table.
So, Is there any way to use * to fetch records in join query?
SELECT Item.ItemId, Item.Item, ItemIndex.CurrentAverageScore, ItemStyle.ItemStyleId, ItemStyle.ItemStyle, Brewery.BreweryId, Brewery.BreweryDescription, (SELECT PersonalRating FROM UserItemJournal WHERE ItemId = ItemIndex.ItemId) As PersonalRating, (SELECT Notes FROM UserItemJournal WHERE ItemId = ItemIndex.ItemId AND PersonalRating IS NOT NULL) As Notes FROM Item, ItemIndex, ItemStyle, Brewery, UserItemJournal WHERE Item.ItemId = ItemIndex.ItemId AND Item.LanguageId = 1 AND ItemStyle.ItemStyleId = ItemIndex.ItemStyleId AND ItemStyle.LanguageId = 1 AND Brewery.BreweryId = ItemIndex.BreweryId AND Brewery.LanguageId = 1 AND UserItemJournal.PersonalRating IS NOT NULL
In above query I want all columns from Item Table.
I think * has limitation, We can use * with single table only.Please correct me if I'm wrong.
Thanks
You can use that table name or aliase name and then dot star (item.*),
and other table columns as you like with their aliase or table name.
SELECT Item.*, ItemIndex.CurrentAverageScore, ItemStyle.ItemStyleId, ItemStyle.ItemStyle, Brewery.BreweryId, Brewery.BreweryDescription, (SELECT PersonalRating FROM UserItemJournal WHERE ItemId = ItemIndex.ItemId) As PersonalRating, (SELECT Notes FROM UserItemJournal WHERE ItemId = ItemIndex.ItemId AND PersonalRating IS NOT NULL) As Notes FROM Item, ItemIndex, ItemStyle, Brewery, UserItemJournal WHERE Item.ItemId = ItemIndex.ItemId AND Item.LanguageId = 1 AND ItemStyle.ItemStyleId = ItemIndex.ItemStyleId AND ItemStyle.LanguageId = 1 AND Brewery.BreweryId = ItemIndex.BreweryId AND Brewery.LanguageId = 1 AND UserItemJournal.PersonalRating IS NOT NULL;