multiply several tables in big one - sql

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?

Related

Insert into new table an id with several values

I've been struggling with this problem.
I have 2 populated tables and a third empty one and I would like to populate the third with data from the other two. Correlate values
The idea is to assign every single ID from the 1st table to entries (several) found in the 2nd table that respect the condition where DestinedUserTypeID should be equal to UserTypeID),
Table 1
Id
UserName
UserTypeID
1
Bla
1
2
Ble
2
3
Bli
3
Table 2
ID
TaskName
DestinedUserTypeID
1
Task A
1
2
Task B
1
3
Task C
1
4
Task D
2
DESIRED TABLE, Table 3
ID
UserID
TaskID
1
1
1
2
1
2
3
1
3
...
Insert into Table3 ([UserId],[TaskID])
SELECT
Id,
(SELECT [Id] FROM [Table2] t2
WHERE [Id] <= 5 AND [DestinedUserTypeId] = 1)
FROM t1 WHERE [UserTypeId] = 1
Thank you!
You can do it with a simple join linked to your table 1 and 2 and just insert into your table3
SELECT
u.id,
t.id,
from
table1 as u inner join
table2 as t on u.userTypeId = t.DestinedUserTypeID

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

SQL Join on 2 Tables

i've been trying to build this query. hoping someone can help.
I have 2 tables.
1 table contains
Code | name | Value | Period
1 name1 1 2010
2 name2 2 2010
table 2 contains
code | name |
1 name1
2 name2
3 name3
4 name4
what i want to be displayed is
1 name1 1
2 namw2 2
3 name3 0
4 name4 0
In some instances table 1 may have a value for all name variables in table 2
but where there are only 1,2,3 names i want it to display the other one but with a value of 0 or blank.
Try this:
select
T2.*,
isnull(T1.code, 0) as code -- or value
from
table2 T2
left outer join table1 T1 on T1.name = T2.name
You can replace isnull(T1.code, 0) as code with isnull(T1.value, 0) as value. I'm not sure what you're after ...

SQL: All rows of two tables merged together

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

Get ID pairs between 2 tables with matching child records

I have 2 tables with the same structure.
FIELD 1 INT
FIELD 2 VARCHAR(32) -- is a MD5 Hash
The query has to get matching FIELD 1 pairs from for records that have the exact combination of values for FIELD 2 in both TABLE 1 and TABLE 2.
These tables are pretty large ( 1 million records between the two ) but are deduced down to an ID and a Hash.
Example data:
TABLE 1
1 A
1 B
2 A
2 D
2 E
3 G
3 H
4 E
4 D
4 C
5 E
5 D
TABLE 2
8 A
8 B
9 E
9 D
9 C
10 F
11 G
11 H
12 B
12 D
13 A
13 B
14 E
14 A
The results of the query should be
8 1
9 4
11 3
13 1
I have tried creating a concatenated string of FIELD 2 using a correlated sub-query and FOR XML PATH string trick I read on here but that is very slow.
You can try following query also -
SELECT t_2.Field_1, t_1.Field_1 --1
FROM table_1 t_1, table_2 t_2 --2
WHERE t_1.Field_2 = t_2.Field_2 --3
GROUP BY t_1.Field_1, t_2.Field_1 --4
HAVING COUNT(*) = (SELECT COUNT(*) --5
FROM Table_1 t_1_1 --6
WHERE t_1_1.Field_1 = t_1.Field_1) --7
AND COUNT(*) = (SELECT COUNT(*) --8
FROM Table_2 t_2_1 --9
WHERE t_2_1.Field_1 =t_2.Field_1) --10
Edit
First the requested set of result is the combination of Field1 from both the tables where respective Field2 is exactly same.
so for that you can use one method which I have posted above.
Here
query will take the data from both the table based on field2 values (from line 1 to line 3)
then it will group the data based on field1 from table1 and field1 from table2 (line 4)
till this step you will get the result having field1 from table1 and field2 from table2 where it exists (at least one) matching based on field2 from tables for respective field1 values.
after this you just need to filter the result for correct (exactly same values for field2 values for respective field1 column value). so that you can make condition on row count.
here my assumption is that you don't have multiple values for field1 and field2 combination in either tables
means following rows will not be present -
1 b
1 b
In any of the tables.
if so, the rows count got for table1 and table2 for same field2 values should be match with the rows present in table1 for field1 and same rows only should present in tables2 for field2 value.
for this condition query has condition on count(*) in having clause (from line 5 to line 10).
Let me try to explain this version of the query:
select t1.field1 as t1field1, t2.field1 as t2field1
from (select t1.*,
count(*) over (partition by field1) as NumField2
from table1 t1
) t1 full outer join
(select t2.*,
count(*) over (partition by field1) as NumField2
from table2 t2
) t2
on t1.field2 = t2.field2
where t1.NumField2 = t2.NumField2
group by t1.Field1, t2.Field1
having count(t1.field2) = max(t1.NumField2) and
count(t2.field2) = max(t2.NumField2)
(which is here at SQLFiddle).
The idea is to compare the following counts for each pair of field1 values.
The number of field2 values on each.
The number of field2 values that they share.
All of these have to be equal.
Each subquery counts the number of values of field2 on each field1 value. For the first rows of your data, this produces:
1 A 2
1 B 2
2 A 3
2 D 3
2 E 3
. . .
And for the second table
8 A 2
8 B 2
9 E 3
9 D 3
9 C 3
Next, the full outer join is applied, requiring a match on both the count and the field2 value. This multiplies the data, producing rows such as:
1 A 2 8 A 2
1 B 2 8 B 2
2 A 3 NULL NULL NULL
2 D 3 9 D 3
2 E 3 9 E 3
NULL NULL NULL 9 C 3
And so on for all the possible combinations. Note that the NULLs appear due to the full outer join.
Note that when you have a pair, such as 1 and 8 that match, there are no rows with NULL values. When you have a pair with the same counts but they don't match, then you have NULL values. When you have a pair with different counts, they are filtered out by the where clause.
The filtering aggregation step applies these rules to get pairs that meet the first condition but not the second.
The having essentially removes any pair that has NULL values. When you count() a column, NULL values are not included. In that case, the count() on the column is fewer than the number of values expected (NumField2).