SQL update statement inserting PeopleID into child tables to link data - sql

I need to place the PeopleID in several tables for my new database to link all of the peole information. I have tried several times to write a simple update statement please help. Every time I get close I get AMBIGUOUS COLUMN ERROR I don't know what else to do.
Update CONTRACT
Set PeopleID = B.PeopleID
from People A
Inner join
(
Select PeopleId, F.ContractID
From People A
Inner Join Person PRSN on PRSN.PersonID = A.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id
) B on A.PeopleID = B.PeopleID
Go

try this
Update CONTRACT Set CONTRACT.PeopleID = B.PeopleID
from People A
Inner join (
Select A.PeopleId, F.ContractID
From People AA
Inner Join Person PRSN on PRSN.PersonID = AA.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id ) B
on A.PeopleID = B.PeopleID
you were asigning the ´A´ alias twice so I recomend using different aliases always

Related

Use name of controllers to find table name in Select

I want to extract a ID , User_ID value from one of the Companies and Contract tables, depending on the ContorollerName value.
select P.TitleProject, P.StartDateProject, P.EndDateProject,P.ControllerID,P.RecordID,P.IsAllocated,P.ProjectStatus_ID,
CN.ControllerName,CN.PersianName,
PU.ProjectID,PU.UserID,PU.RoleID,
CASE
WHEN CN.ControllerName = 'Company' THEN
Companies.Id,Companies.[User_Id]
WHEN CN.ControllerName = 'Contract' THEN
Contracts.Id,Contracts.[User_Id]
END
from Projects P
left outer join Controllers CN ON P.ControllerID = CN.Id
left outer join ProjectUsers PU ON P.Id = PU.ProjectID
where P.IsAllocated = 1
For example, if ContorollerName is 'Company' , the select command is as follows :
select P.TitleProject, P.StartDateProject, P.EndDateProject,P.ControllerID,P.RecordID,P.IsAllocated,P.ProjectStatus_ID,
CN.ControllerName,CN.PersianName,
PU.ProjectID,PU.UserID,PU.RoleID,
Companies.Id,Companies.[User_Id]
You are on the right track -- using left join. But you need to add the tables to the from clause with the appropriate logic.
The logic for the join is quite unclear. The query looks something like this:
select . . .,
coalesce(c.id, co.id) as id,
coalesce(c.user_id, co.user_id) as user_id
from Projects P left join
Controllers CN
on P.ControllerID = CN.Id left join
ProjectUsers PU
on P.Id = PU.ProjectID left join
companies c
on c.? = ? and -- no idea what the right join conditions are
c.ControllerName = 'Company' left join
contracts co
on co.? = ? and -- no idea what the right join conditions are
co.ControllerName = 'Contract'
where P.IsAllocated = 1

SQL query with multiple where clause

I'm trying to write a query for a dataset in SSRS. I have a joining table called RequestSchools which links to three other tables:
Schools
Placing Request
RequestSchoolType
I want to get the name of the RequestedSchool (RequestedSchoolTypeId = 3)
I also want the name of the CatchmentSchool (RequestSchoolTypeId = 1)
I've attached an image of the SQL query that I can get working with the requested school. I want the catchment school as well. Grateful for any help with this.
SELECT
pr.PlacingRequestId, s.Name AS RequestedSchool
FROM
PlacingRequests AS pr
INNER JOIN
RequestSchools AS rs ON rs.PlacingRequestId = pr.PlacingRequestId
JOIN
Schools s ON s.SchoolId = rs.SchoolId
WHERE
rs.RequestSchoolTypeId = 3
I'm not 100% sure this is correct, as we can't see the schema, source data or a full example of the desired output, but I think this might be it:
SELECT
pr.PlacingRequestId,
s.Name as RequestedSchool,
cs.Name as CatchmentSchool
FROM
PlacingRequests AS pr
INNER JOIN RequestSchools AS rs
ON rs.PlacingRequestId = pr.PlacingRequestId
INNER JOIN Schools s
on s.SchoolId = rs.SchoolId
AND rs.RequestSchoolTypeId = 3 -- requested school
INNER JOIN Schools cs
on cs.SchoolId = rs.SchoolId
AND rs.RequestSchoolTypeId = 1 -- catchment school
Happy to update if it's not what you meant, and you can clarify the question as indicated.
According what I understand about your requirement, you will be needing multiple joins and not another AND condition in the where clause. So we can omit the where here and join the requestschools with schools one more time for CatchmentSchool as we have done it once already for Requestedschool. Only difference is we are using the requestedtypeid and catchmenttypeid values while joining.
Select
pr.PlacingRequestId,
rs.Name as RequestedSchool
cs.Name as CatchmentSchool
FROM
PlacingRequests AS pr
INNER JOIN RequestSchools AS rrs
ON rrs.PlacingRequestId = pr.PlacingRequestId
and rrs.RequestSchoolTypeId = 3
JOIN Schools rs
on rs.SchoolId = rrs.SchoolId
INNER JOIN RequestSchools AS crs
ON crs.PlacingRequestId = pr.PlacingRequestId
and crs.RequestSchoolTypeId = 1
JOIN Schools cs
on cs.SchoolId = crs.SchoolId
Hope this helps...
The best way to do this is with a CASE function, but I don't know which RDBMS you are using so here is the brute force approach: just run your query as two sub-queries, each specifying a different type ID and then join them. It would be something like this:
SELECT DISTINCT catch.placingrequestid, catch.catchmentschool, requested.requestedschool
FROM
(SELECT pr.placingrequestid, s.nam as catchmentschool
FROM (PlacingRequest pr
INNER JOIN RequestSchools rs ON pr.placingrequestid = rs.placingrequestid)
INNER JOIN School s ON rs.schoolid = s.schoolid
WHERE rs.requestschooltypeid = 1) catch
INNER JOIN
(SELECT pr.placingrequestid, s.nam as requestedschool
FROM (PlacingRequest pr
INNER JOIN RequestSchools rs ON pr.placingrequestid = rs.placingrequestid)
INNER JOIN School s ON rs.schoolid = s.schoolid
WHERE rs.requestschooltypeid = 3) requested
ON catch.placingrequestid = requested.placingrequestid

sql inner join over multiple tables

I've given this relational scheme and following task:
Inner Join: Return a list of professors, which gives
'lehrveranstaltung' of the 'fachbereich' with the name 'informatik'.
* print 'vorname', 'ho_name', 'lv_name'
* output should sort surnames in ascending order and if they're the same in descending order
* identical lines should online shown once
now I came up with following query:
select distinct
v.vorname,
h.ho_name,
l.lv_name
--print wanted, only once
from
vorname v,
hochschulangehoeriger h,
lehrveranstaltung l
-- from these tables
inner join fachbereich f on f.fb_name = 'Informatik'
-- only the 'informatik' events
inner join prof_haelt_lv on l.lv_nr = pl.lv_nr
-- make sure 'lehrveranstaltung' is from a professor
inner join mitarbeiter mit on pl.pers_Nr = mit.pers_Nr
-- make sure dude is a prof
where
mit.ho_nr = h.ho_nr
and
mit.ho_nr = v.ho_nr -- give only names from prof
order by
2 asc,
3 desc; -- order rules
I think this works for me (can't test it properly). But when I look at it I'll wish that I came up for a bether solution since this looks kinda ugly and wrong for me.
Is there a bether way of doing this? (Have to use inner join)
Based on the table you have, you may use the following SQL statement
SELECT DISTINCT v.vorname,
h.ho_name,
l.lv_name
FROM vorname v
INNER JOIN hochschulangehoeriger h
ON v.ho_nr = h.ho_nr
INNER JOIN mitarbeiter m
ON m.ho_nr = h.ho_nr
INNER JOIN fachbereich f
ON f.fb_nr = m.fb_nr
AND f.fb_name = 'Informatik'
INNER JOIN lehrveranstaltung l
ON l.fb_nr = f.nb_nr
INNER JOIN professor p
ON p.pers_nr = m.pers_nr
INNER JOIN prof_haelt_lv pl
ON pl.pers_nr = p.pers_nr
AND pl.lv_nr = l.lv_nr
ORDER BY 2,
3 DESC;
Also, these section on your SQL, this has no connection to any table in your SQL
inner join fachbereich f on f.fb_name = 'Informatik'
-- only the 'informatik' events
you forgot the alias for prof_haelt_lv
inner join prof_haelt_lv on l.lv_nr = pl.lv_nr
-- make sure 'lehrveranstaltung' is from a professor

Left join not returning record with null

Here's the structure of my database (used to collect info from a survey):
question_responses->answer_choices->subquestions->questions->survey
| ^
V |
submissions->response_group<----------------------------survey_deployment
where A->B means A has the PK of B
So, I want to get the responses of a particular user; let's say that his submission pk is 1. I had written the query
select question_responses.answer_text
from submissions join question_responses on (question_responses.submission_pk = submission.pk)
join answer_choices on (question_responses.answer_choices_pk = answer_choices.pk)
join subquestions on (answer_choices.subquestions_pk = subquestions.pk)
right join questions on (subquestions.questions_pk = questions.pk)
where submissions.pk = 1
order by questions.order
It is possible to not answer questions. In such a case, there is no question_responses.pk recorded. I would still like to be able to account for skipping, so I need this record to be returned, even if there is no response. I thought that the right join would have accounted for this record, but apparently not.
Any help is greatly appreciated.
Because you have submissions.pk = 1 in the where clause this effectively turns your outer join into an inner join, since any missing submission will have a pk of null. and null = 1 is not true.
You could rewrite your query using a LEFT JOIN, and selecting from questions:
select question_responses.answer_text
from questions
left join (subquestions
inner join answer_choices
on answer_choices.subquestions_pk = subquestions.pk
inner join question_responses
on question_responses.answer_choices_pk = answer_choices.pk
inner join submissions
on question_responses.submission_pk = submission.pk)
on subquestions.questions_pk = questions.pk
and submissions.pk = 1
order by questions.order;
This is similar to doing something like:
select subquery.answer_text
from questions
left join
( select question_responses.answer_text, subquestions.questions_pk
from subquestions
inner join answer_choices
on answer_choices.subquestions_pk = subquestions.pk
inner join question_responses
on question_responses.answer_choices_pk = answer_choices.pk
inner join submissions
on question_responses.submission_pk = submission.pk
where submissions.pk = 1
) subquery
on subquery.questions_pk = questions.pk
order by questions.order;
but depending on your dbms the former may perform better as it has no derived tables.
Check out this, does it help?
SELECT question_responses.answer_text
FROM questions LEFT JOIN subquestions on (subquestions.questions_pk = questions.pk)
LEFT JOIN answer_choices on (answer_choices.subquestions_pk = subquestions.pk)
LEFT JOIN question_responses on (question_responses.answer_choices_pk = answer_choices.pk)
LEFT JOIN submissions on (question_responses.submission_pk = submission.pk)
WHERE submissions.pk = 1
ORDER BY questions.order

SQL Server update with joins

I am trying to update a date in a table, based off of a MAX(date) in another table. To get the correct data to link up, I have to do 2 inner joins and 2 left outer joins.
I can select the correct data, it returns a Guid (PersonId) and the Date.
I have to use this information to update my original table. I am having trouble getting this to work, I still getting syntax errors.
update tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
set assign.dtmQAPCLed = dtmTaken
from
(
select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken from tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as types on types.intcerttypeid = certs.intcerttypeid
where types.intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid as data
)
where data.guidpersonid = team.guidpersonid
Assuming you are using SQL Server for this, then this should work:
UPDATE A
SET A.dtmQAPCLed = dtmTaken
FROM tblqualityassignments AS A
INNER JOIN tblrequirementteams as T
ON A.guidmemberid = T.guidmemberid
INNER JOIN (select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken
from tblqualityassignments as assign
inner join tblrequirementteams as team
on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg
on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs
on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as [types]
on [types].intcerttypeid = certs.intcerttypeid
where [types].intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid) data
ON T.guidpersonid = data.guidpersonid