I have 3 tables.
table_main:
+-----+----------+-------------+
| _id | ENTRY_ID | DATA |
+-----+----------+-------------+
| 1 | a1 | some data 1 |
| 2 | a2 | some data 2 |
| 3 | a3 | some data 3 |
+-----+----------+-------------+
table_additions:
+----------+-----------+
| ENTRY_ID | TIMESTAMP |
+----------+-----------+
| a1 | 123456 |
| a2 | 123458 |
| a1 | 123654 |
| a1 | 123658 |
| a2 | 123843 |
| a3 | 123911 |
+----------+-----------+
table_deletions:
+----------+-----------+
| ENTRY_ID | TIMESTAMP |
+----------+-----------+
| a3 | 123556 |
| a2 | 123558 |
| a3 | 123754 |
| a1 | 123858 |
| a3 | 123863 |
| a3 | 123976 |
+----------+-----------+
I am working in android. I want to get the data from table_main of all the entries which have :
number of occurances in table_additions > number of occurances in
table_deletions
So, in the above example, the output should be:
+-----+----------+-------------+
| _id | ENTRY_ID | DATA |
+-----+----------+-------------+
| 1 | a1 | some data 1 |
| 2 | a2 | some data 2 |
+-----+----------+-------------+
This can be done with correlated subqueries:
SELECT *
FROM table_main
WHERE (SELECT COUNT(*)
FROM table_additions
WHERE entry_id = table_main.entry_id
) >
(SELECT COUNT(*)
FROM table_deletions
WHERE entry_id = table_main.entry_id
)
Related
I am working on a project which requires me to do some query in android database. However, I am completely new to database and I am a little confused at how to deal with such scenarios. Here's a rough example to show my question.
For a table like this:
TableA:
--------------------------------------------------------
| _id | name | mimetype | data1 | data2 | data3 |
--------------------------------------------------------
| 1 | user1 | mime1 | val1 | val2 | x |
--------------------------------------------------------
| 2 | user1 | mime2 | val3 | val4 | y |
--------------------------------------------------------
| 3 | user1 | mime3 | val8 | val5 | a |
--------------------------------------------------------
| 4 | user2 | mime2 | val6 | val7 | q |
--------------------------------------------------------
| 5 | user2 | mime3 | val9 | val10 | a |
--------------------------------------------------------
| 6 | user3 | mime1 | val11 | val12 | b |
--------------------------------------------------------
Basically, I want to do a query to return data from columns of name, data1, data2 from TABLE A when it fits the following criteria:
mimetype = mime2
there's another row in the table that has the same name and its mimetype = mime3 and data3 = a
I am wondering how to do this in one query.
If anyone can give some direction on how to figure this out, it will be great! Thanks!
This can be done with the EXISTS operator and a correlated subquery:
SELECT ...
FROM TableA
WHERE mimetype = 'mime2'
AND EXISTS (SELECT 1
FROM TableA AS T2
WHERE T2.name = TableA.name
AND T2.mimetype = 'mime3'
AND T2.data3 = 'a');
I have two autocomplettextview and 3 columns in my table col1,col2,col3. col1 is primary key
table look like this:
| col1 | col2 | col3 |
-----------------------------------------
| 1 | store1 |apple,pineapple,mango |
| 2 | store2 | apple,orange |
| 3 | store3 | apple,pineapple,orange|
| 4 | store4 | orange,mango |
| 5 | store5 | mango,jackfruit |
I have two questions:
Q1: can i store multiple values in single column.If No then how can I store?
Q2: previously said i have two autocompletetextview to search
I.e.:
If user types apple and pineapple in two autocompletetextview then I need to
display row that matches these two texts.
Expected o/p:
| col1 | col2 | col3 |
-----------------------------------------
| 1 | store1 |apple,pineapple,mango |
| 2 | store3 | apple,pineapple,orange|
how can I achieve this?
I gone through several reseaches, with the help of foreign key I can store multiple values in single column, but how can I use foreign key in Where clause to achieve expected o/p.
You shouldnt store multiple values in same column. Instead you should use three tables.
Then is easy to find what StoreID have both food
SELECT FoodAvailable.StoreID
FROM FoodAvailable
JOIN Foods
ON FoodAvailable.FoodID = Foods.FoodID
WHERE Foods.Food in ('apple', 'pineapple')
GROUP BY FoodAvailable.StoreID
HAVING count(*) = 2
Store
| StoreID | StoreName |
-----------------------
| 1 | store1 |
| 2 | store2 |
| 3 | store3 |
| 4 | store4 |
| 5 | store5 |
Food
| FoodID | Food |
-----------------------
| 1 | apple |
| 2 | pineapple |
| 3 | mango |
| 4 | orange |
| 5 | jackfruit |
FoodAvailable
| StoreID | FoodID |
---------------------
| 1 | 1 | //apple
| 1 | 2 | //pineapple
| 1 | 3 | //mango
| 2 | 1 | //apple
| 2 | 4 | //orange
| 3 | 1 | //apple
| 3 | 2 | //pineapple
| 3 | 4 | //orange
| 4 | 4 | //orange
| 4 | 3 | //mango
| 5 | 3 | //mango
| 5 | 5 | //jackfruit
Your Final query is something like this
SELECT f.StoreID, GROUP_CONCAT(food ORDER BY food DESC SEPARATOR ', ')
FROM FoodAvailable f
WHERE f.StoreID = (SELECT FoodAvailable.StoreID
FROM FoodAvailable
JOIN Foods
ON FoodAvailable.FoodID = Foods.FoodID
WHERE Foods.Food in ('apple', 'pineapple')
GROUP BY FoodAvailable.StoreID
HAVING count(*) = 2
)
GROUP BY f.StoreID
I want to find out difference of sum of debit and credit transactions of user and list them out.
Here is the query which i have tried out:
SELECT *, (val1 - val2)
FROM (SELECT *, sum(transactionAmount) AS val1
FROM tableTransaction
WHERE creditedToNum = My_Number
GROUP BY debtToNum) db
JOIN (SELECT *, sum(transactionAmount) AS val2
FROM tableTransaction
WHERE debtToNum = My_Number
GROUP BY creditedToNum) Cr
WHERE db.transactionStatus = 1
AND db.isActive = 1;
It's returning 0 rows but the actual output should be as below
Here is my table structure:
| Amount | Credit_to | Debit_to |
|--------|-----------|-----------|
| 2000 | My_Number | Number_1 |
| 5000 | My_Number | Number_2 |
| 3000 | Number_1 | My_Number |
| 4000 | Number_2 | My_number |
| 2000 | My_Number | Number_2 |
What actual result I want is:
| Name | Amount |
|----------|--------|
| Number_1 | 1000 |
| Number_2 | 3000 |
There is no join condition in your query.
Instead of joining, it might be a better idea to use a compound query to bring the data into a useful form:
SELECT Credit_to AS Name, Amount
FROM tableTransaction
WHERE Debit_to = #My_Number
UNION ALL
SELECT Debit_to, -Amount
FROM tableTransaction
WHERE Credit_to = #My_Number;
| Name | Amount |
|----------|--------|
| Number_1 | 3000 |
| Number_2 | 4000 |
| Number_1 | -2000 |
| Number_2 | -5000 |
| Number_2 | -2000 |
Then just group it:
SELECT Name, SUM(Amount) AS Total
FROM (SELECT Credit_to AS other, Amount
FROM tableTransaction
WHERE Debit_to = #My_Number
UNION ALL
SELECT Debit_to AS other, -Amount
FROM tableTransaction
WHERE Credit_to = #My_Number)
GROUP BY Name
ORDER BY Name;
| Name | Total |
|----------|-------|
| Number_1 | 1000 |
| Number_2 | -3000 |
I have a table like below,
|Product |Brand |Order |Description |
---------------------------------------------------------
|TTT | CCC | 1 | FFF |
|EEE | DDD | 2 | DDD |
|ZZZ | BBB | 99 | NULL |
|NNN | FFF | 99 | NULL |
|QQQ | EEE | NULL | NULL |
|BBB | FFF | 1 | FFF |
|BBB | GGG | 1 | CCC |
|GGG | NULL | NULL | NULL |
|MMM | BBB | 99 | NULL |
I want to order by Order asc, Description asc, Brand asc, Product asc,
but Order null and 99 should be considered as one group and then sort by Brand and Product.
Expected Result should be like below,
|Product | Brand |Order |Description |
---------------------------------------------------------
|BBB | GGG | 1 | CCC |
|TTT | CCC | 1 | FFF |
|BBB | FFF | 1 | FFF |
|EEE | DDD | 2 | DDD |
|MMM | BBB | 99 | NULL |
|ZZZ | BBB | 99 | NULL |
|QQQ | EEE | NULL | NULL |
|NNN | FFF | 99 | NULL |
|GGG | NULL | NULL | NULL |
Always the null goes first or last, but I need a query to use different field sorting according to each condition.
Such exceptions can be handled with CASE expressions.
When Order is NULL, it should be handled like 99, so replace NULL with 99.
When Order is NULL or 99, the description should be ignored, so in this case, replace Description with a fixed value:
SELECT ...
ORDER BY CASE WHEN "Order" IS NULL THEN 99 ELSE "Order" END,
CASE WHEN "Order" IS NULL OR "Order" = 99 THEN 0 ELSE Description END,
Brand,
Product;
I have 7 tables, each table has a column for total I need to get the total of each table which has the same id number, these are 3 sample tables:
clean table
clean_id | labor_expense | machine_expense | diesel_expense | clean_total
1 | 1000 | 2000 | 500 | 3500 2 | 2000 | 1000 | 1000 | 4500
plant_table
plant_id | labor_expense | machine_expense | plant_expense | plant_total
1 |
1000 |
2000 |
500 | 3500 2 | 2000 | 1000 | 1000 | 4500
fertilize_table
fertilize_id | labor_expense | machine_expense | fertilizer_expense | fertilize_total
1 |
1000 |
2000 |
500 | 3500 2 | 2000 | 1000 | 1000 | 4500
How can I get the total of clean_total, plant_total and fertilize_total that have the same id number?
I was thinking to save it to another table like total_expense but I only get the total of the first id number
total_expenses
total_id | clean_total | plant_total | fertilize_total | total_expenses
1 |
3500 |
3500 |
3500 | 10500 2 | 4500 | 4500 | 4500 | 13500
If the rows for each ID are known to exist in all tables, you can simply join them:
SELECT clean_id AS total_id,
clean_total + plant_total + fertilize_total AS total_expenses
FROM clean_table
JOIN plant_table ON clean_id = plant_id
JOIN fertilize_table ON clean_id = fertilize_id