I have 2 tables that i join with LEFT JOIN function tableA & tableB
- data1,col1 and data2,col2 are the references column I use to match both tables entries
- data3 is a number I use to sum with value from TableB.col3
- data4 is the value I want to update based on the sum of tableA.data3 + tableB.col3
TableA TableB
data1,data2,data3,data4 col1,col2,col3
10001,Feb-2019,100, 10001,Feb-2019,43,
10001,Mar-2019,201, 10001,Mar-2019,22,
10002,Feb-2019,123, 10003,April-2019,23,
10003,April-2019,53,
...
COMMAND:
SELECT data1, data2, data3, col3 ,data3+col3 from tableA tA LEFT JOIN tableB tB ON tA.data1=tB.col1 AND tA.data2=tB.col2;
10001|Feb-2019|100|43|143
10001|Mar-2019|201|22|223
10002|Feb-2019|123||
10003|April-2019|53|23|76
...
I can get the desire result with the SELECT statement
SELECT data1, data2, data3, IFNULL(col3,0) ,data3+IFNULL(col3,0) from tableA tA LEFT JOIN tableB tB ON tA.data1=tB.col1 AND tA.data2=tB.col2;
10001|Feb-2019|100|43|143
10001|Mar-2019|201|22|223
10002|Feb-2019|123|0|123
10003|April-2019|53|23|76
...
I want the result of the SUM to be set in tableA.data4 to get the following restult
SELECT * from tableA;
10001|Feb-2019|100|143
10001|Mar-2019|201|223
10002|Feb-2019|123|123
10003|April-2019|53|76
Many thanks
You can do it like this:
UPDATE TableA
SET data4 = data3 + COALESCE((
SELECT IFNULL(tB.col3, 0)
FROM tableB tB
WHERE TableA.data1=tB.col1 AND TableA.data2=tB.col2
), 0);
See the demo.
Results:
| data1 | data2 | data3 | data4 |
| ----- | ---------- | ----- | ----- |
| 10001 | Feb-2019 | 100 | 143 |
| 10001 | Mar-2019 | 201 | 223 |
| 10002 | Feb-2019 | 123 | 123 |
| 10003 | April-2019 | 53 | 76 |
Related
I tried the following but got the wrong result back.
select tab_2.id_3,tab_2.name from tab_2 where tab_2.id_3 in (select id_1 from tab_1 where id_1= id_2)
What I'm looking for is:
I have the first table where I extracted the ids I want:
tab_1:
id_1 | id_2
ab01 | ab01
ab02 | ab02
ab03 | ab05
ab04 | ab09
Select id_1 from tab_1 where id_1= id_2
--id_1--
ab01
ab02
tab_2
id_3|name
ab01|test
ab02|test
ab07|test
ab06|test
Expected join result from tab_1 & tab_2
id_1 | id_3 | name
ab01 | ab01 | test
ab02 | ab02 | test
Thanks for your help.
Join the tables and apply the condition in the where clause:
select t1.id_1, t2.id_3, t2.name
from tab_1 t1 inner join tab_2 t2
on t2.id_3 = t1.id_1
where t1.id_1 = t1.id_2
See the demo.
Results:
| id_1 | id_3 | name |
| ---- | ---- | ---- |
| ab01 | ab01 | test |
| ab02 | ab02 | test |
Solved!
The problem I had was the output. In one table the IDs were capital and in the second table IDs were lower case. So I converted one of them to either lower case or capital.
lower(ids)
upper(ids)
Pictorial Presentation
select tab_2.id_3,tab_2.name from tab_2 where tab_2.id_3 in (select id_1 from tab_1 where id_1= id_2)
I have such kind of a data:
table1
id | part | price
1 | ox900 | 100
2 | ox980 | 200
and
table2
id | part | price
1 | ox560 | 560
2 | ox980 | 120
as result I want to get such schema:
id | part | priceTable1 | priceTable2 | minPrice
1 | ox900 | 100 | | 100
1 | ox980 | 200 | 120 | 120
1 | ox560 | | 560 | 560
to simplify it can be without minPrice column...
now I have such query:
SELECT *
FROM (select part, price from supportContacts
union all
select part, price from supportContacts2)
group by part
but it's not exactly what I want to achieve.
Is it possible somehow to do, what I've described above?
Also a fiddle: http://sqlfiddle.com/#!7/f7401/7/0
SQLite does not support full outer joins, so get a list of all parts first, and then look up their prices with left outer joins:
SELECT part,
table1.price AS priceTable1,
table2.price AS priceTable2,
min(ifnull(table1.price, 'inf'),
ifnull(table2.price, 'inf')) AS minPrice
FROM (SELECT part FROM table1
UNION
SELECT part FROM table2)
LEFT JOIN table1 USING (part)
LEFT JOIN table2 USING (part);
(fiddle)
Given three tables with one table serving as a junction table which contains two foreign key columns, I'm trying to make an insert so that, given a TableA.prefix, TableA.number, TableB.prefix, and TableB.number, I can update the JunctionTable.is_archived column for the matching row in JunctionTable:
So while the matching row in JunctionTable currently looks like:
+----------------------------------------------------------------------+
| id | tblA_id | tblB_id | is_archived |
| 3 | 7 | 98 | 0 |
+----------------------------------------------------------------------+
And matching rows in TableA and TableB look like:
TableA
+----------------------------------------------+
| id | prefix | number |
| 7 | CLA | 754 |
+----------------------------------------------+
TableB
+----------------------------------------------+
| id | prefix | number |
| 98 | RED | 221 |
+----------------------------------------------+
I'd like to UPDATE the is_archived value like so:
+----------------------------------------------------------------------+
| id | tblA_id | tblB_id | is_archived |
| 3 | 7 | 98 | 1 |
+----------------------------------------------------------------------+
I've tried a few different statements based on information found here but they aren't valid:
UPDATE JunctionTable
SET is_archived = "1"
WHERE tblAid =
(SELECT id FROM TableA WHERE prefix = "CLA" AND number = 754)
AND tblB.id =
(SELECT id FROM TableB WHERE prefix = "RED" AND number = 221)
UPDATE JunctionTable
SET is_archived = "1"
WHERE (
LEFT JOIN TableA ON JunctionTable.tblA_id=TableA.id
WHERE TableA.course_prefix = "CLA" AND TableA.course_number = 754
LEFT JOIN TableB ON JunctionTable.tblB_id=TableB.id
WHERE TableB.course_prefix = "RED" AND TableB.course_number = 221)
In the first query, it looks like the problems are the names of the ID columns in your Junction table ("tblAid" and "tblB.id"), and you're using double quotes instead of single quotes. This should work:
UPDATE JunctionTable
SET is_archived = 1
WHERE tblA_id =
(SELECT id FROM TableA WHERE prefix = 'CLA' AND number = 754)
AND tblB_id =
(SELECT id FROM TableB WHERE prefix = 'RED' AND number = 221)
I have an sqlite db with two table
table1
------------------------------
TIME | ElevationA| ElevationB|
-----|-----------|-----------|
T1 | eA1 | eB1 |
T2 | eA2 | eB2 |
table2
------------------------------
TIME | Temperat A| Temperat B|
-----|-----------|-----------|
T1 | tA1 | tB1 |
T2 | tA2 | tB2 |
I am searching for a "magic" command that make a table of all parameter at a given time, e.g something that would be like:
SELECT WHERE TIME=T1 table1 AS ELEV ,table2 AS TEMP
and that would result in
table3
------------
ELEV | TEMP |
-----|----- |
eA1 | tA1 |
eB1 | tB1 |
Of course I could bash script it but I would prefer a to create a view in SQLite as it is more straightforwards and avoid to duplicate the data.
Any idea welcome
You can use:
CREATE TABLE TABLE3(ELEV,TEMP);
INSERT INTO TABLE3(ELEV,TEMP) VALUES((SELECT TIME FROM TABLE1 WHERE TIME = T1),SELECT TIME FROM TABLE2 WHERE TIME =T2));
These 2 select clauses must return the same number of records.
I have an android sqllite database. It has a text column called chainid.
I'd like to return all columns from rows with DISTINCT chainids || or where chainid is equal to: "none".
So e.g.:
| ID| Name | chainid |
| 1 | widgetname1 | 12345 |
| 2 | widgetname2 | 12345 |
| 3 | widgetname3 | "none" |
| 4 | widgetname4 | 49390 |
| 5 | widgetname5 | 49390 |
Given the above table I would like my query to return 3 rows with all columns for row 2, row3 and row5. -- So DISTINCT on chainid OR where chainid = "none" with the max id selected as the distinct row
Can I achieve this in one query?
I could return all and then process afterwards in java, but this is inefficient.
What about
select *
from table where id in
( select max(id)
from table
group by chainid
where chainid != 'none'
union
select id
from table
where chainid = 'none'
)