SQL: All rows of two tables merged together - sql

I am trying to combine two different tables in a select statement where all the rows in the first table are matched with all the rows in the second table. For example:
Table1
Table1_ID | FKey_Table2_ID
1 9
2 null
Table2
Table2_ID | Table2_Value
9 Yes
10 No
11 Maybe
Results needed:
Table1_ID | FKey_Table2_ID | Table2_ID | Table2_Value
1 9 9 Yes
1 null 10 No
1 null 11 Maybe
2 null 9 Yes
2 null 10 No
2 null 11 Maybe
Please note that the first row in Table1 has a key already assigned from Table2.

This is called a cross join and can be accomplished like this:
SELECT Table1_ID, FKey_Table2_ID, Table2_ID, Table2_Value
FROM Table1
CROSS JOIN Table2
Or more simply
SELECT Table1_ID, FKey_Table2_ID, Table2_ID, Table2_Value
FROM Table1, Table2

SELECT * FROM Table1
CROSS JOIN
Table2

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

Update rows in a table based on grouping

I have a table with records of the same type with ID's and a grouping. I need to update the id's of table 1 into table 2 based on the record id from largest to smallest using the position.
Table 1
ID
Group
Name
1
A
Apple
2
A
Apple
3
B
Apple
4
B
Apple
Table 2
ID
recordid
position
group
1
1
250
A
2
2
350
A
3
null
450
A
4
null
550
A
5
3
250
B
6
4
350
B
7
null
450
B
8
null
550
B
update table2
set recordid=T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
This isn't distinct so I don't think it's right, but I can't figure it out.
where ?????
the problem is you have multiple Id per group , so you have to choose one :
update table2
set recordid= ( select top 1 T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
)
where recordid is null

Join two tables, using value from the first unless it is null, otherwise use value from the second

I have three tables which look like those:
TABLE 1
id j_id
1 1
2 2
3 3
TABLE 2
id j_id table1_id
1 57 1
2 84 1
3 1 1
4 9 2
5 2 2
and every j has a value in a third table
id value
1 1abc
2 2bcd
3 3abc
57 57abc
84 84abc
9 9abc
I am trying to write a query which will join table 1 and table 2 and use the J value from the third table instead of the j_id, but the problem is that I want to use the j value from the second table if it exists and otherwise use the value from the first table.
in order the make it clearer this is my query result without using the third table:
tbl1.j_id tbl2.j_id
1 1
1 84
1 57
2 2
2 9
3 null
I want the end query result to use the second table's j value unless it is null:
tbl1.j_id tbl2.j_id j_id
1 1 1abc
1 84 84abc
1 57 57abc
2 2 2abc
2 9 9abc
3 null 3abc
(Question and title edits are more than welcome, weren't that sure how to phrase them..)
You can simply JOIN to table3 on the COALESCE of table2.j_id and table1.j_id:
SELECT t1.j_id AS t1_j_id, t2.j_id AS t2_j_id, t3.value
FROM table1 t1
LEFT JOIN table2 t2 ON t2.table1_id = t1.id
JOIN table3 t3 ON t3.id = COALESCE(t2.j_id, t1.j_id)
Output:
t1_j_id t2_j_id value
1 1 1abc
1 57 57abc
1 84 84abc
2 2 2bcd
2 9 9abc
3 null 3abc
Demo on dbfiddle
One solution is to left join table3 twice:
select
t1.j_id,
t2.j_id,
coalesce(t31.value, t32.value) j_value
from
table1 t1
left join table2 t2 on t2.table1_id = t1.id
left join table3 t31 on t31.id = t2.j_id
left join table3 t32 on t32.id = t1.j_id

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?

sql with two table and group and result from that table?

I have two tables. I need to take away from the first table second table. Stim to another table without all the rows of the first table.
Table1
Table2
Result = Table1(value1) – Table2(value1) -----groupe no. 2 or no.1
Result (groupe no. 2)
Result
id value1 groupe
_______________________
1 10 2
2 9 2
3 10 2
5 5 2
6 11 2
7 12 2
I need the result of which I can write the group number and get result for that group.
Try this query:
Select
t1.id,
t1.value1-t2.value1 as value,
t1.groupe from
table1 t1,table2 t2
where t1.id=t2.id and t1.groupe=2;
try this:
SELECT
T1.id, T2.group, T1.valor - T2.valor AS value
FROM
Table1 T1 INNER JOIN
Table2 T2 ON T1.id = T2.id AND T1.group = T2.group
WHERE (T1.group = 2)