Insert primary keys from two newly created tables in a relationship table - sql

I have one table which contained 4 records (PersonName, CityName, CityState, CityCountry) in two different tables. One of the tables now has personID, personName and the other one has Cityid, CityName, CityState, CityCountry.
Now I created third table which hold PersonId, CityId. How can I populate that table with the ids of person and city from the original table since they are split now. I want to get the ids from the newly created tables based on the relationship they had in the original table.

Can you not just join back to the original table?
INSERT PersonCity (PersonID, CityID)
SELECT p.PersonID, c.CityID
FROM OriginalTable o
INNER JOIN Person p
ON p.PersonName = o.Personname
INNER JOIN City c
ON c.CityName = o.CityName
AND c.CityState = o.CityState
AND c.CityCountry = o.CityCountry;

Related

Create a additional row from join sql?

I have 2 tables: Person and House with 1-n relation.
I want to the result return as picture below:
Row always have a Person column with a null House column.
Thanks.
you can use unionall to join the result set with person table something like
select p.name,h.name as housename from person p join house h on p.id=h.personid
union all (select name,null from person)
order by name,housename

Join tables and add column only with a specific value

I have four tables (a, b, c, d) in PostgreSQL database:
I can connect these tables with subject.id and project.id unique keys. My query is:
SELECT subject_id, firstname, lastname, national_id, project_title, project_id, note_template
FROM project_subject
JOIN subject ON subject.id = project_subject.subject_id
JOIN project ON project.id = project_subject.project_id
JOIN subject_note ON subject_note.subject_id = project_subject.subject_id
WHERE project_id = xxxx
My problem is that in table d (subject_notes) one subject can have several notes (varchar) plus selectable note_template (int). I would like to have joined table with columns subject_id, firstname, lastname, national_id, project_title, project_id and note_template. When I join four tables I get result with several same subject rows with different notes. I would like to have table where each subject is listed only one time and to the last column value is added only when note_template=16. Rest of the subjects would have null value for note_template.
Change the last join to a LEFT join and add the condition note_template = 16 in the ON clause:
LEFT JOIN subject_note ON subject_note.subject_id = project_subject.subject_id
AND subject_note.note_template = 16

joining a table on 2 fields

I want to pull a person and their supervisor names from a table. The persons table has the supervisor_id and the person_id. The names table has name_id and a Full Name field. If I join Person ON either supervisor_id or person_id, how do I get the other to display as well?
You need to join twice, one for each relationship you have:
SELECT
-- Persons' columns
P.*,
-- Superviser name columns
SN.*,
-- Person name columns
PN.*
FROM
persons AS P
LEFT JOIN names AS SN ON P.supervisor_id = SN.name_id
LEFT JOIN names AS PN ON P.person_id = PN.name_id
Or you can join with an OR clause, but you won't be able to know which record did you join with unless you check with a CASE.
SELECT
-- Persons' columns
P.*,
-- name columns
N.*,
IsSupervisor = CASE WHEN P.supervisor_id = N.name_id THEN 'Yes' ELSE 'No' END
FROM
persons AS P
LEFT JOIN names AS N ON
P.supervisor_id = N.name_id OR
P.person_id = N.name_id
This last approach will display 2 rows as it will match either one or the other on different occasions, not both with the same persons row (as the first example).
A (self)join is what you need:
select p.*, supervisor=ps.name
from Person p join person ps on p.supervisor_id=ps.id

JOIN Two tables one of which has where clause

I want to join two tables where Person table fields(having Street S1) will be merged into Student table, but adding new field (STUDENT/NONSTUDENT).
Student table has 1milyon rows, Result person table has max 100 rows.
What is the best sql for performance to merge them all?
student table (name, age)
A-12
B-23
C-24
person table (name, street, live)
A-S1-L
B-S2-NL
D-S1-L
At the end I want such result
A-12-Student
D-NULL-NOTSTUDENT
This should work:
select p.name,
s.age,
case when s.name is null then 'NotStudent'
else 'Student' end as IsStudent
from person p
left join student s on p.name = s.name
where p.Street = 's1'

Table with multiple columns containing employee keys. How to link to employee master to get names?

My table has columns of data that contain employee keys for different departments. One column, for example would be business office, another admin.
I can't figure out how to write and sql that will join the employee master to return each persons name for each row. I could do it if it only had one column of employee master keys but it beat me
I think from what you have described you need to join to the Employee Master table multiple times:
SELECT A.Col1
, A.BOEmpID
, BO.EmpName
, A.AdminEmpID
, AD.EmpName
, <....>
FROM MyTable A
INNER JOIN Employees BO
ON A.BOEmpID = BO.EmpID
INNER JOIN Employees AD
ON A.AdminEmpID = AD.EmpID