I am trying to retrieve data for a client from an old database that they have. The data I need is a mix of these 2 sql statements. How would I combine them to get all the data from the enrol_stud table and Stud_Haddress table in the second statement.
Statement 1 - I want all the data from Enrol_Stud and Stud_Haddress included in statement 2
select enrol_stud.*, stud_haddress.*
from enrol_stud_forms
inner join enrol_stud on enrol_stud_forms.student_id = enrol_stud.student_id
Statement 2 I want the data from the 2 tables above included in the statement below.
SELECT contact.*, vsmc.*, concarer1.*, salcarer2.*, contact.firstname ||'
'||contact.surname as STUDENT, salcarer1.salutation as CARER1_TITLE, concarer1.firstname as CARER1_FIRSTNAME, concarer1.surname as CARER1_SURNAME, concarer1.email_address as CARER1_EMAIL, salcarer2.salutation as CARER2_TITLE, concarer2.firstname as CARER2_FIRSTNAME, concarer2.surname as CARER2_SURNAME, concarer2.email_address as CARER2_EMAIL
FROM get_currently_enroled_students ('now') gces
INNER JOIN student on gces.student_id = student.student_id
INNER JOIN contact on student.contact_id=contact.contact_id
INNER JOIN view_Student_mail_carers vsmc on student.student_id=vsmc.student_id
INNER JOIN contact concarer1 on vsmc.carer1_contact_id=concarer1.contact_id
INNER JOIN contact concarer2 on vsmc.carer2_contact_id=concarer2.contact_id
INNER JOIN salutation salcarer1 on concarer1.salutation_id=salcarer1.salutation_id
INNER JOIN salutation salcarer2 on concarer2.salutation_id=salcarer2.salutation_id
ORDER BY contact.surname, contact.firstname
I know it is in the joins, I just can't work out how to include them.
You can try like below, include the columns from first statement per post as well include the joins as pointed below
SELECT contact.*,
vsmc.*,
enrol_stud.*, <- here
stud_haddress.*, <-here
concarer1.*,
salcarer2.*,
contact.firstname ||' '||contact.surname as STUDENT,
salcarer1.salutation as CARER1_TITLE,
concarer1.firstname as CARER1_FIRSTNAME,
concarer1.surname as CARER1_SURNAME,
concarer1.email_address as CARER1_EMAIL,
salcarer2.salutation as CARER2_TITLE,
concarer2.firstname as CARER2_FIRSTNAME,
concarer2.surname as CARER2_SURNAME,
concarer2.email_address as CARER2_EMAIL
FROM get_currently_enroled_students ('now') gces
INNER JOIN enrol_stud_forms on gces.student_id = enrol_stud_forms.student_id <- here
INNER JOIN enrol_stud on enrol_stud.student_id = gces.student_id <- here
INNER JOIN student on gces.student_id = student.student_id
INNER JOIN contact on student.contact_id=contact.contact_id
INNER JOIN view_Student_mail_carers vsmc on student.student_id=vsmc.student_id
INNER JOIN contact concarer1 on vsmc.carer1_contact_id=concarer1.contact_id
INNER JOIN contact concarer2 on vsmc.carer2_contact_id=concarer2.contact_id
INNER JOIN salutation salcarer1
on concarer1.salutation_id=salcarer1.salutation_id
INNER JOIN salutation salcarer2
on concarer2.salutation_id=salcarer2.salutation_id
ORDER BY contact.surname, contact.firstname
Related
Goal:
I wish to get the Count of how many times a WorkItem was re-assigned
From what I understand the proper query is the following:
SELECT
WorkItemDimvw.Id,
COUNT(WorkItemAssignedToUserFactvw.WorkItemAssignedToUser_UserDimKey) AS Assignments
FROM WorkItemDimvw INNER JOIN WorkItemAssignedToUserFactvw
ON WorkItemDimvw.WorkItemDimKey = WorkItemAssignedToUserFactvw.WorkItemDimKey
GROUP BY WorkItemDimvw.Id
The EXISTING query is below and I'm wondering / forgeting if I should:
Just add in COUNT(WorkItemAssignedToUserFactvw.WorkItemAssignedToUser_UserDimKey) AS Assignments since joins are existing, except it is group by WorkItemDimvw.Id
Should it instead be a subquery in the Select below?
Query:
SELECT
SRD.ID,
SRD.Title,
SRD.Description,
SRD.EntityDimKey,
WI.WorkItemDimKey,
IATUFact.DateKey
FROM
SLAConfigurationDimvw
INNER JOIN SLAInstanceInformationFactvw
ON SLAConfigurationDimvw.SLAConfigurationDimKey = SLAInstanceInformationFactvw.SLAConfigurationDimKey
RIGHT OUTER JOIN ServiceRequestDimvw AS SRD
INNER JOIN WorkItemDimvw AS WI
ON SRD.EntityDimKey = WI.EntityDimKey
LEFT OUTER JOIN WorkItemAssignedToUserFactvw AS IATUFact
ON WI.WorkItemDimKey = IATUFact.WorkItemDimKey
AND IATUFact.DeletedDate IS NULL
The trick is to aggregate the data on a sub query, before you join it.
SELECT
SRD.ID,
SRD.Title,
SRD.Description,
SRD.EntityDimKey,
WI.WorkItemDimKey,
IATUFact.DateKey,
IATUFact.Assignments
FROM
SLAConfigurationDimvw
INNER JOIN
SLAInstanceInformationFactvw
ON SLAConfigurationDimvw.SLAConfigurationDimKey = SLAInstanceInformationFactvw.SLAConfigurationDimKey
RIGHT OUTER JOIN
ServiceRequestDimvw AS SRD
ON <you're missing something here>
INNER JOIN
WorkItemDimvw AS WI
ON SRD.EntityDimKey = WI.EntityDimKey
LEFT OUTER JOIN
(
SELECT
WorkItemDimKey,
DateKey,
COUNT(WorkItemAssignedToUser_UserDimKey) AS Assignments
FROM
WorkItemAssignedToUserFactvw
WHERE
DeletedDate IS NULL
GROUP BY
WorkItemDimKey,
DateKey
)
IATUFact
ON WI.WorkItemDimKey = IATUFact.WorkItemDimKey
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
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
So here's a challenge. I have a webpage that needs to show data from 5 different tables in a database. Here's the query:
'SELECT ORGANIZATIONS.ORG_NAME, ORG_SECTIONS.SectionName, ATTORNEYS.NAME, ATTORNEYS.LASTNAME, ATTORNEYS.EMAIL, ATTORNEYS.TEL, ATTY_TITLES.ATT_TITLE, FIRM_PRACTICE_GRPS.PRACTICE_GRP
FROM (ORGANIZATIONS INNER JOIN (Org_Sec_Atty INNER JOIN ATTORNEYS ON Org_Sec_Atty.Atty_ID = ATTORNEYS.ATTY_ID) ON ORGANIZATIONS.ID = Org_Sec_Atty.OrgID) INNER JOIN ORG_SECTIONS ON Org_Sec_Atty.SecID = ORG_SECTIONS.ID
WHERE ATTORNEYS.LASTNAME LIKE #LASTNAME;'
The 5 tables are Org_Sec_Atty, ORGANIZATIONS, ORG_SECTIONS, ATTORNEYS, ATTY_TITLES, and FIRM_PRACTICE_GRPS. First, I didn't come up with the table names, lol.
Second, ATTORNEYS is the table that has ATTY_ID, TITLE_ID, PRACTICE_GRP_ID, which is what I'm sure I need to join to FIRM_PRACTICE_GRPS and ATTY_TITLE, and then it needs to connect with the Org_Sec_Atty and ORG_SECTIONS table.
I already have a page where the three tables ATTORNEYS, Org_Sec_Atty, and ORG_SECTIONS are joined by the JOIN statement you see up there. I'm trying to figure out where the other INNER JOIN statements would go and in what order.
It seems like I need to replace "ATTORNEYS" in the "Org_Sec_Atty INNER JOIN ATTORNEYS" with the subordinate INNER JOIN statements, but, again, I'm not sure of the order. Is this possible?
You didn't specified foreign keys on last two tables. Change appropriately if necessary:
SELECT ORGANIZATIONS.ORG_NAME ,
ORG_SECTIONS.SectionName ,
ATTORNEYS.NAME ,
ATTORNEYS.LASTNAME ,
ATTORNEYS.EMAIL ,
ATTORNEYS.TEL ,
ATTY_TITLES.ATT_TITLE ,
FIRM_PRACTICE_GRPS.PRACTICE_GRP
FROM (((( ORGANIZATIONS
INNER JOIN Org_Sec_Atty ON ORGANIZATIONS.ID = Org_Sec_Atty.OrgID)
INNER JOIN ATTORNEYS ON Org_Sec_Atty.Atty_ID = ATTORNEYS.ATTY_ID)
INNER JOIN ORG_SECTIONS ON Org_Sec_Atty.SecID = ORG_SECTIONS.ID)
INNER JOIN ATTY_TITLES ON ORG_SECTIONS.ATTY_ID = ATTY_TITLES.ID)
INNER JOIN FIRM_PRACTICE_GRPS ON ATTY_TITLES.FIRM_ID = FIRM_PRACTICE_GRPS.ID
WHERE ATTORNEYS.LASTNAME LIKE #LASTNAME;
I am joining a query with it self and my code is working ok on ase isql. However when I want to use it in access 2007 I get the following error "The derived table expression is missing a correlation name. Check derived table syntax in the reference manual"
The original code does something like this:
select TT.name, TT.lastname, max(amount) as maxsalecurrentweek
from sales
inner join
(select s1.id_employee, e.name, e.lastname, e.address, e.age, e.id_employee
from employee e
join sales s1 on e.id_emplyoee = s1.id_employee
where
some conditions here) as TT
on sales.id_employee = TT.id_employee
group by
TT.name, TT.lastname
In the original code I join more tables in the inner query as well as some where conditions. But the above code should illustrate what I do.
It looks like the way I join the table with it self is the problem in access. Does anyone know what How is the correct sysntaxis? Or if access/JET/ACE support this inner join with it self approach?
Here is the original code:
select max(tort140.BEL_GRLAG_AP) as MaxPensjonGr, TT.IDE_KUNDE_PRSNR,
TT.DAT_KUNDE_FOEDT_NUM, TT.AvtaleID, TT.Orgnr, TT.Arbeidsgiver,
TT.Sivilstatus, TT.Polisestatus, TT.Årslønn from tort140
inner join
(select distinct tort128.NUM_AVTALE_ID as AvtaleID,
tort009.IDE_ARBGIV_NR as Orgnr,
tort134.NVN_ARBGIV as Arbeidsgiver,
tort127.DAT_KUNDE_FOEDT_NUM as DAT_KUNDE_FOEDT_NUM,
tort127.IDE_KUNDE_PRSNR as IDE_KUNDE_PRSNR,
tort001.STA_SIVILSTATUS as Sivilstatus,
tort128.typ_status as Polisestatus,
tort128.rte_polisegrad as Polisegrad,
tort140.BEL_LOENN_AAR as Årslønn,
tort138.IDE_SEKV_TORT138"
from tort140 left join (tort138 join (tort128 join (tort134 join (tort009 join (tort001 join tort127
on tort127.DAT_KUNDE_FOEDT_NUM=tort001.DAT_KUNDE_FOEDT_NUM and tort127.IDE_KUNDE_PRSNR=tort001.IDE_KUNDE_PRSNR)
on tort127.DAT_KUNDE_FOEDT_NUM=tort009.DAT_KUNDE_FOEDT_NUM and tort127.IDE_KUNDE_PRSNR=tort009.IDE_KUNDE_PRSNR)
on tort009.IDE_ARBGIV_NR=tort134.IDE_ARBGIV_NR)
on tort128.IDE_SEKV_TORT127 = tort127.IDE_SEKV_TORT127)
on tort128.IDE_SEKV_TORT128 = tort138.IDE_SEKV_TORT128)
on tort140.IDE_SEKV_TORT138 = tort138.IDE_SEKV_TORT138)
where tort128.NUM_AVTALE_ID = '102356' and tort128.DAT_GYLDIG_FOM <= 20120101
and (tort128.DAT_GYLDIG_TOM >= 19520000 or tort128.DAT_GYLDIG_TOM is null
and tort128.DAT_HISTORISK is null and tort128.TYP_STATUS= 'akt' and tort127.DAT_KUNDE_FOEDT_NUM >= 19650000
and tort127.DAT_KUNDE_FOEDT_NUM <= 19550000 and tort127.DAT_TERMINERT is null and tort127.DAT_REGISTRERT<= 19550000
and tort009.DAT_SLUTT is null and tort134.DAT_HISTORISK is null
and tort138.DAT_AKSJON=(select max(p.DAT_AKSJON) from tort138 p where 1=1 and p.IDE_SEKV_TORT128=tort128.IDE_SEKV_TORT128)
) as TT
on TT.IDE_SEKV_TORT138 = tort140.IDE_SEKV_TORT138
Group by TT.IDE_KUNDE_PRSNR, TT.DAT_KUNDE_FOEDT_NUM, TT.AvtaleID, TT.Orgnr,
TT.Arbeidsgiver, TT.Sivilstatus, TT.Polisestatus, TT.Årslønn from tort140
The inner query works on access 2007 without any problem. I got the error message when I write the inner join.
Do you think this query can be suited into access?? Am I missing some brakets or something?
You have to add a join type in MS Access, Join on its own will not work.
from employee e
INNER join sales s1 on e.id_emplyoee = s1.id_employee
Edit per comments, the line above is taken from the posted SQL and fits back in like so:
select TT.name, TT.lastname, max(amount) as maxsalecurrentweek
from sales
inner join
(select s1.id_employee, e.name, e.lastname, e.address, e.age, e.id_employee
from employee e
INNER join sales s1 on e.id_emplyoee = s1.id_employee
where
some conditions here) as TT
on sales.id_employee = TT.id_employee
group by
TT.name, TT.lastname