Query to get specific value between two table - sql

I have two table which contains two different primary key, lets call them, table1 and table2.
The tables may have the same number of columns.
Table1:
ID
NOM
CODE
1
AAA
661YYYDD
2
BBB
YYYD661
3
CCC
YD661
4
DDD
P5500Z
Table 2:
ID
KEYCODE
1
661
2
55
I want to be able to get by KEYCODE:
ALL record in table1 which contain 661 or 55. For example when I select by 661 I get only the first 3 rows from tables1.

This works as well:
SELECT *
FROM TABLE1
JOIN TABLE2
ON TABLE1.CODE LIKE '%'||TABLE2.KEYCODE||'%'
WHERE TABLE2.KEYCODE = '661'
dbfiddle

Related

How to separate comma separate values and get aggregate of a column in SQL?

I have a table T1 as below
product_id val
123,567 5
999 4
999 3
and another table T2,
t_product_id // this maps to product_id in above table
123
999
In the final output, for t_product_id in table T2 I have to get value for it from T1. For duplicate product_ids (999) I want to get the min value, and for 123 I want to get 5
This is how output should look like
product_id value
123 5
999 3
My query ->
select t1.product_id, min(t1.value)
from T1 t1
group by t1.product_id
I am not sure what needs to be done next. How to separate comma separated values and check if 123 from T2 exists in T1 and get the value for it
it's not possible to keep only one product_id per row in table T1?
I think this would simplify matters for you. T1 would be:
123 | 5
567 | 5
999 | 4
999 | 3
Use join to only select the ids that exist in T2
select t1.product_id, min(t1.value)
from T1 t1 join T2 on (t1.product_id = t2.t_product_id)
group by t1.product_id

group by id and random sample by id from two tables in big query sql

I have a 2 tables with the same structure:
table1
id text var
1 "bla bla" 100
1 "blabla1" 30
2 "qweweqty" 0
2 etc...
7
3
3
1
..
100
table2
id text var
101 "bla bla" 10
101 "bla1" 60
101 "bla" 5
103 etc...
102
103
102
110
..
200
I want to randomly sample data from table1 and table2 based on id. so basically, sample every observation for a random sample of ids from table1 and and every observation from random sample of ids from table2 so that 50 ids are from table 1 and 50 are from table 2 . any idea on how to do this on big query SQL?
en example would be the following where I want to get 3 ids from table1 and 3 from table2
randomly ids 1,2,3 are selected from table1, ids 101, 110 and 103 are selected from table2
the resulting table is then:
id. text var
1. .. ..
1
2
2
3
3
1
101
101
101
103
103
110
so basically any observation from table1 with id 1,2,3 and any observation from table2 with id 101, 103, 110 are selected and put in the same table:
so the passages are two: first randomly select a certain number of ids from table1, and a certain number of ids from table2, then I select any observation corresponding to those ids from both tables and I join them in the same table
If you want 50 ids from each table, then you can limit them using subqueries:
select t1.*
from t1 join
(select distinct id
from t1
order by rand()
limit 50
) ids
on t1.id = ids.id
union all
select t2.*
from t2 join
(select distinct id
from t2
order by rand()
limit 50
) ids
on t2.id = ids.id

MS ACCESS & VBA Matching Query

I have two tables, I want to match values in both tables/pairing fields as below
In table 1 records 1 and 2 should be updated in MatchRef field as matched
In table 2 records 1 and 2 should be updated in matchref field as matched
but in Table 1 the record 3 should not updated as matched as there is no matching field in table 2
Table 1
ID MatchField MatchValue MatchRef
1 AAA 100
2 AAA 100
3 AAA 100
4 BBB 100
Table 2
ID MatchField MatchValue MatchRef
1 AAA 100
2 AAA 100
4 BBB 100
Here ID is not an unique filed, its just auto number.
How to do it in Ms Access? There will be 600 000 records on average in each table and working on a Citrix environment.
Here is how you would find matching records as far as I can tell --
SELECT
*
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.ID = T2.ID
AND T1.MATCHFIELD = T2.MATCHFIELD

multiply several tables in big one

There is a task about sql.
I have tree tables
Table_1
Id Name
1 Name1
2 Name2
Table_2
Id Name
3 Name3
4 Name4
And final table
Table_3
Id Table1_Id Table2_Id Value
1 1 3 Some Text
2 1 4 Some another text
So i would like two more rows to be generated for table_3
Id Table1_Id Table2_Id Value
1 1 3 Some Text
2 1 4 Some another text
null 2 3 null
null 2 4 null
How can i do this ?
In fact, i have more then 5 tables with same signature as Table_1 and Table_2.
And each time i add one row to the one of this table, the result Table_3's values count should be multiplied.
Is Table_3 created from a JOIN of Table_1 and Table_2?
If so what JOIN are you using?
Have you tried CROSS APPLY or OUTER JOIN?

Merge two tables with summing up values

I have two tables,
Table1:
Key, Value
----------
1 10
2 20
3 30
Table2:
Key, Value
----------
3 30
4 40
5 50
How can merge them and get the following using SQL?
Desired output:
Key, Value
----------
1 10
2 20
3 60
4 40
5 50
In Python terminology, I guess it is called summing up two dictionaries.
PS: I am using Oracle 12c.
Use Union all to combine two select queries and sum the value with group by key column
select key,sum(value) value
from
(
select Key, Value from table1
union all
select Key, Value from table2
) a
group by key
or use Full outer Join
SELECT COALESCE(a.Key, b.key),
COALESCE(a.Value, 0) + COALESCE(b.value, 0)
FROM table1 a
FULL OUTER JOIN Table2 b
ON a.Key = b.Key
For more efficient in terms of speed, as much as possible avoid using UNION/UNION ALL. Try my answer below:
SELECT
NVL(A.Key,B.Key)[Key],
(NVL(A.Value,0) + NVL(B.Value,0))[Value]
FROM Table1 A
FULL JOIN Table2 B ON A.Key=B.Key