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)
Related
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 |
Good afternoon,
I would like to know if it is possible to make a query to generate columns according to the number of rows that I have in my table
example:
ID COD DIAG
111111111 | Z359 | D
111111112 | Z359 | D
111111112 | Z359 | D
111111113 | Z359 | R
111111113 | Z359 | P
111111113 | Z359 | R
111111114 | Z359 | D
111111114 | Z359 | D
111111114 | Z359 | D
111111115 | Z359 | D
it would be ideal that columns be created according to the number of rows for each id, if not possible it would put a fixed number of columns.
result query
ID | COD1 | DIAG1 | COD2 | DIAG2 | COD3 | DIAG3
111111111 | Z359 | D | | | |
111111112 | Z359 | D | Z359 | D | |
111111113 | Z359 | R | Z359 | P | Z359 | R
111111114 | Z359 | D | Z359 | D | Z359 | D
111111115 | Z359 | D | | | |
sorry my english
Thanks a Lot !!
This first query follows the pattern of the answer to the duplicate question, included here for comparison.
WITH numbered AS (
SELECT row_number() OVER
(PARTITION BY ID ORDER BY COD, DIAG)
AS seq,
t.*
FROM SO58566470 t)
SELECT ID,
max(CASE WHEN seq = 1 THEN COD END) AS COD1,
max(CASE WHEN seq = 1 THEN DIAG END) AS DIAG1,
max(CASE WHEN seq = 2 THEN COD END) AS COD1,
max(CASE WHEN seq = 2 THEN DIAG END) AS DIAG1,
max(CASE WHEN seq = 3 THEN COD END) AS COD3,
max(CASE WHEN seq = 3 THEN DIAG END) AS DIAG3
FROM numbered n
GROUP BY ID;
But that really is a naive use of window functions, since it could have maximized the window by calculating other values at the same time. The first query is already collecting and traversing partitioned rows to get the row number, yet it essentially repeats that process twice by collecting values in the next query using the aggregate max() functions.
The following query looks longer and perhaps more complicated, but it takes advantage of the partitioned data (i.e. window data) by collecting the transformed values in the same process. But because window functions necessarily operate on each row, it becomes necessary to filter out "incomplete" rows. I did not do any kind of profiling on the queries, but I suspect this second query is much more efficient overall.
WITH transform AS (
SELECT id,
lag(COD, 0) OVER IDWin AS COD1,
lag(DIAG, 0) OVER IDWin AS DIAG1,
lag(COD, 1) OVER IDWin AS COD2,
lag(DIAG, 1) OVER IDWin AS DIAG2,
lag(COD, 2) OVER IDWin AS COD3,
lag(DIAG, 2) OVER IDWin AS DIAG3,
row_number() OVER IDWin AS seq
FROM SO58566470 t
WINDOW IDWin AS (PARTITION BY ID ORDER BY COD, DIAG)
ORDER BY ID, SEQ
),
last AS (
SELECT id, max(seq) as maxseq
FROM transform
GROUP BY id
)
SELECT transform.*
FROM transform
JOIN last
ON transform.id = last.id AND transform.seq = last.maxseq
ORDER BY id;
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)
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'm trying to get a result from two sqlite tables that contain the same columns but have no other relation. Both have the date and amount columns and all I want is a new table as a result to show dates and amounts from both tables.
Table A
+----------+-------+
| date | amount|
+----------+-------+
|10-01-2013| 3.8 |
|12-23-2104| 4.2 |
+----------+-------+
and
Table B
+----------+-------+
| date | amount|
+----------+-------+
|10-03-2013| 2.4 |
|12-28-2014| 3.5 |
+----------+-------+
And the desired table would be
+----------+----------+---------+
| date | A.amount | B.amount|
+----------+----------+---------+
|10-01-2013| 3.8 | NULL |
|10-03-2013| NULL | 2.4 |
|12-23-2104| 4.2 | NULL |
|12-28-2014| NULL | 3.5 |
+----------+----------+---------+
I tried many posts in the forum but I couldn't find any that match my need.
Could you help?
What you need is not a JOIN is a UNION.
see:
UNIONS in SQLITE
Something like:
SELECT Date, Amount as Amount1, NULL AS Amount2
FROM TableA
UNION ALL
SELECT Date, NULL AS Amount1, Amount as Amount2
FROM TableB