Here is my tables:
T1 record:
id referencePerson1ID referencePerson2ID referencePerson3ID
1 1 2 3
T2 referencePerson:
id name
1 Peter
2 John
3 Mary
I want to get the following result when I choose T1 id =1:
id referencePerson1 referencePerson2 referencePerson3
1 Peter John Mary
How can I do that?
Thanks
This is one basic way to do things:
SELECT T1.id, P1.name, P2.name, p3.name
FROM record T1 LEFT JOIN referencePerson P1
ON T1.referencePerson1ID=P1.id
LEFT JOIN referencePerson P2
ON T1.referencePerson2ID=P2.id
LEFT JOIN referencePerson P3
ON T1.referencePerson3ID=P3.id
Another way when number of persons is unknown can be done using a PIVOT
You can do that with three times JOIN :
SELECT t1.id, t21.name, t22.name, t23.name
FROM T1
INNER JOIN T2 t21 ON t21.id = T1.referencePerson1ID
INNER JOIN T2 t22 ON t22.id = T1.referencePerson2ID
INNER JOIN T2 t23 ON t23.id = T1.referencePerson3ID
WHERE T1.id = 1
Related
I want to combine three tables. Of which two multiple columns have to be joined together.
T1
id
name
1
Carl
2
Max
3
Bob
t2
id
t1_id
lastname
function
5
1
Johnsen
Welder
6
2
Clinten
Carpenter
7
3
Brink
Mason
t3
id
t2-id
Function
9
5
Metalworking
10
6
Cabinet maker
11
7
jointer
result
id
name
lastname
Function
1
Carl
Johnsen
Welder
1
Carl
Johnsen
Metalworking
2
Max
Clinten
Carpenter
2
Max
Clinten
Cabinet maker
3
Bob
Brink
Mason
3
Bob
Brink
jointer
my code is
Select t1.id, t1.naam, t2.lastname , t3.Function
From t1
left join t2 on t2.t1-id=t1.[id]
left join t3 on t3.t2-id=t2.[id],
(Select
Function
from t2
union
select
Function
from t3)t
You need a join with subquery for union based on id
Select
t1.id, t1.naam, t2.lastname, t.Function
From
t1
Left Join
t2 On t2.t1 - id = t1.[id]
Left Join
t3 On t3.t2 - id = t2.[id]
Inner Join
(Select id, Function
From t2
Union
Select id, Function
From t3) t On t.id = t1.id
I am thinking of using union all on two separate queries, one on two tables and one on three tables:
select t1.id, t1.name, t2.lastname, t2.function
from t1 join
t2
on t1.id = t2.t1_id
union all
select t1.id, t1.name, t2.lastname, t3.function
from t1 join
t2
on t1.id = t2.t1_id join
t3
on t2.id = t3.t2_id;
Note that no outer joins are needed. I am guessing that this would be better performing than an option with union/union all.
First of all, I'm an amateur on SQL. Here it is the example. From this three tables I would like to know who are the teachers that make more money than Mike
Table1:
LessonName TeacherID
Maths 3
Biology 2
Biology 4
Geology 1
Table2:
Lesson PricePerClass
Maths 200
Biology 100
Geology 150
Table3:
IDTeacher TeacherName
1 Mike
2 John
3 Lauren
4 Julian
So far I've made this:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
where TotalRevenue > (select TotalRevenue
from Table2 as t2
inner join Table1 as t1 on t2.Lesson = t3.LessonName
inner join Table3 as t3 on t3.IDTeacher = t1.TeacherID
where t3.TeacherName = "Mike");
And I don't know how to keep going because when I run it, an error appears.
My expected result would be something like:
IDTeacher TotalRevenue
3 200
Thanks!
In your main query you must use a HAVING clause instead of a WHERE clause and also in the subquery fix your joins:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
having TotalRevenue > (
select sum(t2.PricePerClass)
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
where t3.TeacherName = "Mike"
);
See the demo.
Results:
| IDTeacher | TotalRevenue |
| --------- | ------------ |
| 3 | 200 |
First of all, I'm an amateur on SQL. Here it is the example. From this three tables I would like to know how is called the teacher who makes more money with the classes:
Table1:
LessonName TeacherID
Maths 3
Biology 2
Biology 1
Geology 1
Table2:
Lesson PricePerClass
Maths 200
Biology 100
Geology 150
Table3:
IDTeacher TeacherName
1 Mike
2 John
3 Lauren
My main problem is that I don't know how to deal with the repeated values from the first table when I'm doing the triple join.
So far "I've made" this:
select IDTeacher, PricePerClass
from Table1 as T1
inner join Table2 as T2 on t1.LessonName = t2.Lesson
inner join Table3 as T3 on t1.TeacherId = t3.idTeacher
...
And I don't know how to keep going. I will have to group the t1.LessonName but every time I try to do it I get syntax errors. As you can see I'm pretty lost.
EDIT: My expected result would be something like:
IDTeacher TotalRevenue
1 250
Thanks a lot.
join the tables, group by teacher to aggregate and get the top row after you sort by the total descending:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
order by TotalRevenue desc limit 1
Note that this query does not return ties, if any.
SELECT t3.IDTeacher, t3.TeacherName, sum(t2.PricePerClass) from table1 t1
inner join table3 t3 on t1.TeacherID = t3.IDTeacher
inner join table2 t2 on t1.Lessonname = t2.Lesson
group by t3.TeacherName
order by sum(t2.PricePerClass) desc limit 1;
I want to get Ronald from Left Table through matching Right Table values with 1 AND 2. I know I need to use DISTINCT to get only one row but other than that, I'm stumped.
Left Table
pid | name
1 Ronald
2 Chris
3 John
Right Table
pid | value
1 1
1 2
2 1
3 2
Joined Table
pid | name | value
1 Ronald 1
1 Ronald 2
2 Chris 1
3 John 2
Expected Output
pid | name
1 Ronald
You want to match both 1 and 2. Aggregation and having come to mind:
select t1.pid, t1.name
from table1 t1 join
table2 t2
on t1.pid = t2.pid
where t2.value in (1, 2)
group by t1.pid, t1.name
having count(*) = 2; -- should be `count(distinct)` if duplicates are possible in `table2`
Self join table2 and select value = 1 in the first and value = 2 in the second. Join the result with table 1.
select t1.pid, t1.name
from table1 t1
join table2 t2_1 on t1.pid = t2_1.pid
join table2 t2_2 on t2_1.pid = t2_2.pid AND t2_1.value = 1 AND t2_2.value = 2
Or if you like that better:
select t1.pid, t1.name
from table1 t1
join table2 t2_1 on t1.pid = t2_1.pid AND t2_1.value = 1
join table2 t2_2 on t2_1.pid = t2_2.pid AND t2_2.value = 2
I have two SQL tables, and I like to have the result shown below.
table_1
id userId
---------------
1 u1
2 u2
3 u3
4 u4
5 u5
table_2
userId flag
-----------------
u1 1
u4 0
Result I need
id userId flag
------------------------
1 u1 1
2 u2 Null
3 u3 Null
4 u4 0
5 u5 Null
You can use this following LEFT JOIN for the purpose, It will give all right table data with matched right table data
.
SELECT * FROM table_1 LEFT JOIN table_2 on table_1.userId=table_2.userId
use the left outer join:
select table_1.id, table_1.userid, table_2.flag
from table_1
left outer join table_2 on table_1.userid=table_2.userid
you can use join or Under-expression
for join :
select
t1.id,
t1.userid,
t2.flag
from table_1 t1
inner join table_2 t2
on t1.userid = t2.userid
under-expression
select
t1.id,
t1.userid,
(select flag from table_2 t2 WHERE t1.userid=t2.userid) as flag
from table_1 t1