How to make sql query using two tables - sql

I have 3 tables T1,T2 and T3. Table T1 contains 2 columns (key,class/student). The column class/student contains both classes and students, for example: 'english', 'math', 'mark', 'tom'... Table T2 contains 2 columns (class,student). Each class have more than one student in it, and these 2 columns use the keys from T1. In Table T3 I want to insert a specific class with its student(s) - class(es) into column A and student(s) into column B. Knowing that these columns use the keys from T1 table I've tried this but it returns same specific class with its students multiple times:
INSERT INTO T3 (A,B)
SELECT m.class, m.student
FROM T1 b,T2 m
WHERE m.class = (SELECT key FROM T1 WHERE class/student='English')
AND b.KEY = m.student;
the result i get: 1 is id of class english ,10 is id of student mark, 11 is id of student tom
table T1:
Table T2:

Since your master table T1 has both class and student details, I would recommend joining it twice with the class_student_map table T2 to get the details:
INSERT INTO T3 (A,B)
SELECT "class_master"."key", "student_master"."key"
FROM
T2 "class_student_map"
INNER JOIN
T1 "class_master"
ON
"class_master"."key" = "class_student_map"."class"
AND
"class_master"."class/student" = 'English'
INNER JOIN
T1 "student_master"
"student_master"."key" = "class_student_map"."student";

Here you have simple code and just one join operation, which positively impacts on performance:
insert into t3
WITH spec_class AS
(select key from T1 where class_student='English')
SELECT m.class, m.student
FROM T2 m inner join spec_class sp on m.class=sp.key;

Related

Insert using selecting column from other table

I have three tables as below
I want to insert the column dep_typ in table 2 as selecting the column from table 1.
But the dep_typ in table 2 has all 4 values as 'U' and whereas in table 1 it is three times 'U' and 1 time 'F'. I want the result to be as same as table 1.
The following will join the dep_type values from t1 with only the records from t2. Your data sample does not have any matching ex_line values common to t1 and t2. Is this true of the entire dataset? It if isn't true for the entire dataset, then #OldProgrammer is right - the join will not return the exact number of records in t1.
SELECT
t1.position,
t1.ss_id,
t2.ex_line,
t1.dep_Typ,
t1.num
FROM
t1
INNER JOIN
t2
ON t1.position = t2.position
AND t1.ss_id = t2.ss_id
INNER JOIN
t3
ON t1.ss_id = t3.ss_id
AND t1.value = t3.value
AND t2.vech_num = t3.vech_num
;

SQL merge results from multiple joined tables into a single view field (without union)

Is there a way to merge/combine the results from multiple joined tables into a single field?
Two key details:
Must not use a Union.
Must be able to generate a View with the code.
We have a nested table structure and there's quite a bit of conditional logic, so I'd like to avoid writing the same large query multiple times (hence why I'm trying to avoid a Union).
select [Combine Id results from all 4 tables]
from Table1 tbl1
inner join Table2 tbl2 on tbl1.Id = tbl2.ParentId
inner join Table3 tbl3 on tbl2.Id = tbl3.ParentId
inner join Table4 tbl4 on tbl3.Id = tbl4.ParentId
So if the tables contain the following data:
Table 1
Id, ParentId
1, 1
Table 2
Id, ParentId
2, 1
Table 3
Id, ParentId
3, 2
Table 4
Id, ParentId
4, 3
Is it possible to produce the following single-field output (list of integers) using the join structure from my original query?:
Id
1
2
3
4
I think apply does what you want:
select v.id
from Table1 tbl1 inner join
Table2 tbl2
on tbl1.Id = tbl2.ParentId inner join
Table3 tbl3
on tbl2.Id = tbl3.ParentId inner join
Table4 tbl4
on tbl3.Id = tbl4.ParentId cross apply
(values (tbl1.id), (tbl2.id), (tbl3.id), (tbl4.id)) v(id);

Deleting duplicates from composite Primary key in SQL server

I am using SQL server 2012.
I have two tables with identical structure. (Say there are four columns - ID, C2, C3, C4)
I want to copy data from Table1 to Table2.
The only difference between the two tables is that Table1 contains the local ID in the 'ID' field and table 2 should have the global ID.
There is another third table that contains this mapping between the localID and the global ID.
(multiple local ID's can be mapped to one Global ID)
Example:
ID 'abcd' in russia can be global ID 123
ID 'cdef' in china can be global ID 123
therefore abcd in russia is basically cdef in china (this mapping is stored in table3)
I want to take the GlobalID from table3 corresponding to the local ID that we have in Table1 and insert them into table2.
Table2 has a primary key constraint defined on the column ID+C2.
The problem is that there are high chance we'll have duplicate data - So we want to insert only distinct combination of ID+C2. (clustered PK)
Can someone please help me with this? Sorry if this is confusing.
I right now have this query, but it doesnt eliminates duplicates when copying data from T1 to T2 and thus I get an error on PK.
INSERT INTO TABLE2
(ID,
C2,
C3,
C4)
SELECT T3.ID,
T1.C2,
T1.C3,
T1.C4
FROM TABLE1 T1
JOIN TABLE3 T3 ON T1.ID = T3.ID
JOIN TABLE2 T2 ON T2.ID = T1.ID
I'm not sure, but I guess you want something like
INSERT INTO Table2 (globalID)
SELECT DISTINCT m.globalID from Mapping m inner join Table1 t
on t.country = m.country and t.localId = m.localID
You can use any select, so alter it for your needs.

Select data from multiple table

I have the following:
Table1 IdTable1, IdTable2, IdTable3
Table2 IdTable2, Title
Table3 IdTable3, FName, LName
I Need the content of Table1 with the Title, FName, LName
I have tried
SELECT T2.Title, T3.FName, T3.LName
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.IdTable2 = T2.IdTable2
LEFT JOIN Table3 T3 ON T1.IdTable3 = T3.IdTable3
All what I get is the Table2 with the other columns NULL or the 2 columns of Table3 with the first column NULL.
Table 1 Contains
1 2 1
2 2 1
3 2 5
Table 2 Contains
1 Mr
2 Madame
Table3 Contains
1 A B
2 C D
3 E F
4 G H
5 I J
The Results of all my Queries are
Madame Null Null
Madame Null Null
Madame Null Null
or
NULL A B
NULL A B
NULL I J
Your SQL is fine as long as you want to return partial results.
Have you verified the actual data is correct? You are using a LEFT JOIN. This means rows in T1 will be returned even if there is no matching data in T2 or T3. If there are no matching rows in one of those tables, the columns in that table will be NULL.
If you include all of the columns in T1 in your result set, you will see that there is indeed data in T1, but there is no matching data in T2 or T3. In cases where T3 columns are NULL, that means there is data in T1 and T2 but not T3.
It looks to me like you've got a referential integrity problem. You may need to add foreign keys with drop constraints or fix some application logic problems when creating or deleting records.
I guess your Table1 is a pivot table for Table2 and Table3. I will do something like below.
SELECT T2.Title, T3.FName, T3.LName
FROM Table1 T1, Table2 T2, Table3 T3 WHERE T2.IdTable2 = T1.IdTable2 and T3.IdTable3 = T1.IdTable3

sql simple 3 table join using same table?

I have two tables:
Table 1
id, name1
Table 2
id, name2a, name2b
Table 2's column names name2a, and name2b are references to table 1's id. I need to create a query that pulls both the names out of table 1 based on the id's used in Table 2.
Therefore, if Table one contained:
1 Peter
2 Paul
And Table 2 contained:
1 1 2
2 2 2
Then a select statement should give me:
Peter Paul
Paul Paul
I've gone around the bend trying to build this SQL and the best I came up with was:
SELECT table1.name AS 'name', table1.name AS 'Other name'
FROM table1, table2
WHERE table1.id = table2.name2a
Which only gives me the name2a column correctly.
Any help appreciated! I guess I need to do a join, but I'm really struggling...
Start with your 2nd table and join TWICE to table 1 (different aliases respectively), then get the name field from each aliased Table1 entry.
select
T2.ID,
TJ1.Name1 as FirstName,
TJ2.Name1 as SecondName
from
Table2 t2
join Table1 TJ1
on t2.Name2a = TJ1.ID
join Table1 TJ2
on t2.Name2b = TJ2.ID
select foo.*, t1.x, t2.y
join t1 on t1.id = foo.a
join t1 as t2 on t2.id = foo.b
If there's a chance that col a or col b is null, use a left join.
Have you tried using an INNER JOIN?
SELECT table1.name AS 'name', table1.name AS 'Other name'
FROM table1 INNER JOIN table2 ON table1.id = table2.name2a;
Sorry if I'm no help, not that great at SQL myself hehe.
Your problem is that you need to reference table1 twice: once for the plain table1.name and again to look up what table2 is pointing at. You can join one table in multiple times if you give them aliases:
SELECT t1.name1, o.name1
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.name2a
JOIN table1 o ON t2.name2b = o.id -- And JOIN back to table1 to get the name1