Sql problem with my query - sql

SELECT
dbo.pi_employee.emp_firstname, dbo.pi_employee.emp_lastname,
dbo.pi_employee.emp_no, dbo.pi_employee.emp_cnic,
dbo.pi_employee.emp_currentadd, dbo.pi_employee.emp_cellph,
dbo.pi_employee.emp_birthday, pi_jobtitle_1.jobtitle_name,
dbo.pi_employee.emp_joindate, dbo.pi_education.edu_degree,
dbo.pi_education.edu_year, dbo.pi_employee.emp_pension,
dbo.pi_employee.emp_age, dbo.pi_employee.emp_service,
dbo.pi_employee.emp_terminate, dbo.pi_employee.emp_termdate,
dbo.pi_employee.emp_basicofpay, dbo.pi_employee.emp_terminationreason,
dbo.pi_employee.emp_terminationdate, dbo.pi_employee.emp_status,
dbo.pi_employee.emp_gender, dbo.pi_employee.emp_maritalstatus,
dbo.pi_employee.emp_paymethod, dbo.pi_employee.emp_leaveentitle,
dbo.pi_employee.emp_confirmation, dbo.pi_employee.emp_title,
dbo.pi_employee.emp_basicamount, dbo.pi_salgrade.salgrade_name,
dbo.tbl_emp_status.StatusName, dbo.pi_skills.skill_type,
dbo.pi_location.loc_name, pi_location_1.loc_name AS wcity,
dbo.pi_jobtitlehist.jthSaleGradetype, dbo.pi_workexp.exp_serperiod,
dbo.pi_employee.emp_domicile, dbo.pi_skills.skill_type AS Skill,
dbo.pi_skills.skill_exp, dbo.pi_education.edu_degree AS Degree,
dbo.pi_education.edu_uni, dbo.pi_education.edu_distinction,
dbo.pi_lochistory.lhstart_date, dbo.pi_lochistory.lhend_date
FROM
dbo.pi_location
RIGHT OUTER JOIN
dbo.pi_workexp
RIGHT OUTER JOIN
dbo.pi_employee ON dbo.pi_workexp.emp_no = dbo.pi_employee.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitlehist ON dbo.pi_employee.emp_no = dbo.pi_jobtitlehist.emp_no ON
dbo.pi_location.loc_id = dbo.pi_employee.emp_location_id
LEFT OUTER JOIN
dbo.pi_salgrade ON dbo.pi_employee.emp_salgrade_id = dbo.pi_salgrade.salgrade_id
LEFT OUTER JOIN
dbo.tbl_emp_status ON dbo.pi_employee.emp_status = dbo.tbl_emp_status.StatusID
LEFT OUTER JOIN
dbo.pi_skills ON dbo.pi_employee.emp_no = dbo.pi_skills.emp_no
LEFT OUTER JOIN
dbo.pi_location AS pi_location_1
INNER JOIN
dbo.pi_lochistory ON pi_location_1.loc_id = dbo.pi_lochistory.loc_id ON dbo.pi_employee.emp_no = dbo.pi_lochistory.emp_no
LEFT OUTER JOIN
dbo.pi_education ON dbo.pi_employee.emp_no = dbo.pi_education.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitle AS pi_jobtitle_1 ON dbo.pi_employee.emp_jobtitle_id = pi_jobtitle_1.jobtitle_id
I am writing sql query to implement different scenario, but problem is that it gives repeated values. I write distinct and order by too but result was same can any one help me to solve this issue.
EDIT – The same query with table names aliased:
SELECT
em.emp_firstname, em.emp_lastname,
em.emp_no, em.emp_cnic,
em.emp_currentadd, em.emp_cellph,
em.emp_birthday, jt.jobtitle_name,
em.emp_joindate, ed.edu_degree,
ed.edu_year, em.emp_pension,
em.emp_age, em.emp_service,
em.emp_terminate, em.emp_termdate,
em.emp_basicofpay, em.emp_terminationreason,
em.emp_terminationdate, em.emp_status,
em.emp_gender, em.emp_maritalstatus,
em.emp_paymethod, em.emp_leaveentitle,
em.emp_confirmation, em.emp_title,
em.emp_basicamount, sg.salgrade_name,
es.StatusName, s.skill_type,
L.loc_name, L1.loc_name AS wcity,
jh.jthSaleGradetype, we.exp_serperiod,
em.emp_domicile, s.skill_type AS Skill,
s.skill_exp, ed.edu_degree AS Degree,
ed.edu_uni, ed.edu_distinction,
Lh.lhstart_date, Lh.lhend_date
FROM
dbo.pi_location AS L
RIGHT OUTER JOIN
dbo.pi_workexp AS we
RIGHT OUTER JOIN
dbo.pi_employee AS em ON we.emp_no = em.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitlehist jh ON em.emp_no = jh.emp_no ON L.loc_id = em.emp_location_id
LEFT OUTER JOIN
dbo.pi_salgrade AS sg ON em.emp_salgrade_id = sg.salgrade_id
LEFT OUTER JOIN
dbo.tbl_emp_status AS es ON em.emp_status = es.StatusID
LEFT OUTER JOIN
dbo.pi_skills AS s ON em.emp_no = s.emp_no
LEFT OUTER JOIN
dbo.pi_location AS L1
INNER JOIN
dbo.pi_lochistory AS Lh ON L1.loc_id = Lh.loc_id ON em.emp_no = Lh.emp_no
LEFT OUTER JOIN
dbo.pi_education ed ON em.emp_no = ed.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitle AS j ON em.emp_jobtitle_id = j.jobtitle_id

Yeah, since you're doing a crap ton of Cartesian products with those joins without on clauses, that will cause plenty of what seem like 'repeated values', but are really just the product of the two tables combining sets.

Related

SQL Case statement with multi left outer joins

Is it possible to do case statements and to left outer joins like I am doing below? I put comments on what I am trying to achieve.
SELECT
eventGame.Id,
eventGame.[Type] AS GameType
FROM GrassrootsHoops.EventGame eventGame
LEFT OUTER JOIN (SELECT
divisionGameTeamResult.Id,
divisionGameTeamResult.GameId,
COALESCE(divisionPool.Name, divisionTeamPoolPoolBracket.Name) AS DivisionPoolName
FROM GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
LEFT OUTER JOIN
-- CASE eventGame.[Type] = 1 THEN
GrassrootsHoops.DivisionTeamPoolGame divisionTeamPoolGame
ON divisionTeamPoolGame.Id = divisionGameTeamResult.Id
LEFT OUTER JOIN GrassrootsHoopS.DivisionTeamPool divisionTeamPool
ON divisionTeamPool.Id = divisionTeamPoolGame.DivisionTeamPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionPool
ON divisionPool.Id = divisionTeamPool.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPoolSettings divisionPoolSettings
ON divisionPool.Id = divisionPoolSettings.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeam
ON divisionTeam.Id = divisionTeamPool.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team team
ON team.Id = divisionTeam.Id
LEFT OUTER JOIN
-- CASE eventGame.[Type] = 2 THEN
GrassrootsHoops.DivisionBracketParticipant divisionBracketGameParticipant
ON divisionBracketGameParticipant.DivisionGameTeamResultId = divisionGameTeamResult.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantPart divisionBracketParticipantPart
ON divisionBracketParticipantPart.Id = divisionBracketGameParticipant.DivisionBracketParticipantPartId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantTeam divisionBracketGameParticipantTeam
ON divisionBracketGameParticipantTeam.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeamBracket
ON divisionTeamBracket.Id = divisionBracketGameParticipantTeam.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team teamBracket
ON teamBracket.Id = divisionTeamBracket.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantBracket divisionBracketParticipantBracket
ON divisionBracketParticipantBracket.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracket divisionBracketParticipantBracketBracket
ON divisionBracketParticipantBracketBracket.Id = divisionBracketParticipantBracket.DivisionBracketId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantPool divisionBracketGameParticipantPool
ON divisionBracketGameParticipantPool.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionPoolBracket
ON divisionPoolBracket.Id = divisionBracketGameParticipantPool.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantTeamPool divisionBracketGameParticipantTeamPool
ON divisionBracketGameParticipantTeamPool.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeamPool divisionTeamPoolBracket
ON divisionTeamPoolBracket.Id = divisionBracketGameParticipantTeamPool.DivisionTeamPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionTeamPoolPoolBracket
ON divisionTeamPoolPoolBracket.Id = divisionTeamPoolBracket.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeamPoolTeamBracket
ON divisionTeamPoolTeamBracket.Id = divisionTeamPoolBracket.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team teamPoolTeamBracket
ON teamPoolTeamBracket.Id = divisionTeamPoolTeamBracket.Id
WHERE divisionGameTeamResult.HomeTeam = 1) homeTeam
ON homeTeam.GameId = eventGame.GameId
You can't do case statements with outer joins. I think you can do what you want, though by using a UNION. Here's an example using a simpler schema where we are doing two separate joins based on the type of game.
SELECT
Game.Id,
PoolResult.Score
FROM
Game
JOIN PoolResult
ON PoolResult.GameId = Game.Id
WHERE
Game.GameType = 1
UNION
SELECT
Game.Id,
BracketResult.Score
FROM
Game
JOIN BracketResult
ON BracketResult.GameId = Game.Id
WHERE
Game.GameType = 2
(Here's a working version at SQL Fiddle)
For your query, you just want to write two completely separate queries one for each type of game. Once you have that working for each separate query, just stick a UNION in between them and the results will be concatenated together. Here's your query modified to use a UNION. I don't have an easy way to check it for errors, so there's probably some minor errors
SELECT
divisionGameTeamResult.Id,
eventGame.[Type] AS GameType
FROM
GrassrootsHoops.EventGame eventGame
JOIN GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
ON (divisionGameTeamResult.GameId = eventGame.GameId
JOIN GrassrootsHoops.DivisionTeamPoolGame divisionTeamPoolGame
ON divisionTeamPoolGame.Id = divisionGameTeamResult.Id
JOIN GrassrootsHoopS.DivisionTeamPool divisionTeamPool
ON divisionTeamPool.Id = divisionTeamPoolGame.DivisionTeamPoolId
JOIN GrassrootsHoops.DivisionPool divisionPool
ON divisionPool.Id = divisionTeamPool.DivisionPoolId
JOIN GrassrootsHoops.DivisionPoolSettings divisionPoolSettings
ON divisionPool.Id = divisionPoolSettings.Id
JOIN GrassrootsHoops.DivisionTeam divisionTeam
ON divisionTeam.Id = divisionTeamPool.DivisionTeamId
JOIN GrassrootsHoops.Team team
ON team.Id = divisionTeam.Id
WHERE
divisionGameTeamResult.HomeTeam = 1
AND
eventGame.[Type] = 1
UNION
SELECT
divisionGameTeamResult.Id,
eventGame.[Type] AS GameType
FROM
GrassrootsHoops.EventGame eventGame
JOIN GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
ON (divisionGameTeamResult.GameId = eventGame.GameId)
JOIN GrassrootsHoops.DivisionBracketParticipant divisionBracketGameParticipant
ON divisionBracketGameParticipant.DivisionGameTeamResultId = divisionGameTeamResult.Id
JOIN GrassrootsHoops.DivisionBracketParticipantPart divisionBracketParticipantPart
ON divisionBracketParticipantPart.Id = divisionBracketGameParticipant.DivisionBracketParticipantPartId
JOIN GrassrootsHoops.DivisionBracketParticipantTeam divisionBracketGameParticipantTeam
ON divisionBracketGameParticipantTeam.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionTeam divisionTeamBracket
ON divisionTeamBracket.Id = divisionBracketGameParticipantTeam.DivisionTeamId
JOIN GrassrootsHoops.Team teamBracket
ON teamBracket.Id = divisionTeamBracket.Id
JOIN GrassrootsHoops.DivisionBracketParticipantBracket divisionBracketParticipantBracket
ON divisionBracketParticipantBracket.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionBracket divisionBracketParticipantBracketBracket
ON divisionBracketParticipantBracketBracket.Id = divisionBracketParticipantBracket.DivisionBracketId
JOIN GrassrootsHoops.DivisionBracketParticipantPool divisionBracketGameParticipantPool
ON divisionBracketGameParticipantPool.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionPool divisionPoolBracket
ON divisionPoolBracket.Id = divisionBracketGameParticipantPool.DivisionPoolId
JOIN GrassrootsHoops.DivisionBracketParticipantTeamPool divisionBracketGameParticipantTeamPool
ON divisionBracketGameParticipantTeamPool.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionTeamPool divisionTeamPoolBracket
ON divisionTeamPoolBracket.Id = divisionBracketGameParticipantTeamPool.DivisionTeamPoolId
JOIN GrassrootsHoops.DivisionPool divisionTeamPoolPoolBracket
ON divisionTeamPoolPoolBracket.Id = divisionTeamPoolBracket.DivisionPoolId
JOIN GrassrootsHoops.DivisionTeam divisionTeamPoolTeamBracket
ON divisionTeamPoolTeamBracket.Id = divisionTeamPoolBracket.DivisionTeamId
JOIN GrassrootsHoops.Team teamPoolTeamBracket
ON teamPoolTeamBracket.Id = divisionTeamPoolTeamBracket.Id
WHERE
divisionGameTeamResult.HomeTeam = 1
AND
eventGame.[Type] = 1

Left outer join "The correlation name 'MMP_PPRVU' has the same exposed name as table 'MMP_PPRVU'."

I've been struggling with this for some time now. It's something to do with how I'm joining the MMP_PPRVU I believe. Essentially, I'm trying to join CLARITY_TDL_TRAN --> CLARITY_EAP1 --> MMP_PPRVU. As well as CLARITY_TDL_TRAN --> MMP_STAGING_UNITS --> MMP_PPRVU.
All left outer joins. I've tried multiple different approaches but cannot come up with a solution that works. Any help is greatly appreciated.
SELECT........
FROM
CLARITY.dbo.CLARITY_TDL_TRAN as CLARITY_TDL_TRAN
left outer join
dbo.MMP_STAGING_UNITS as MMP_STAGING_UNITS on MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID and CLARITY_TDL_TRAN.DETAIL_TYPE in (1, 10)
left outer join
CLARITY.dbo.CLARITY_EAP as CLARITY_EAP on CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
left outer join
CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1 on CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
left outer join
CLARITY.dbo.CLARITY_POS as CLARITY_POS on CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
left outer join
CLARITY.dbo.CLARITY_DEP as CLARITY_DEP on CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
left outer join
CLARITY.dbo.CLARITY_LOC as CLARITY_LOC on CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
left outer join
dbo.mmp_PatientsTestaYes as mmp_PatientsTestaYes on CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
left outer join
CLARITY.dbo.CLARITY_SA as CLARITY_SA on CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
left outer join
CLARITY.dbo.CLARITY_SER as CLARITY_SER on CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
left outer join
CLARITY.dbo.CLARITY_EAF_TIN as CLARITY_EAF_TIN ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
left outer join
dbo.MMP_PPRVU as MMP_PPRVU on MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
left outer join
dbo.MMP_revctrs$ as MMP_revctrs$ ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
left outer join
MMP_PPRVU ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
WHERE (CLARITY_TDL_TRAN.DETAIL_TYPE IN (1, 10)) AND (mmp_PatientsTestaYes.PAT_ID IS NULL) AND (CLARITY_EAF_TIN.LINE = 1)
When you give a table an alias in your FROM clause, it's superflous to name the alias the same as the table name.
LEFT OUTER JOIN dbo.MMP_PPRVU AS MMP_PPRVU
ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
Furthermore, you join in the same table a few JOIN's down and it's name is the same. One of these two tables will need to have an alias that is different from the table name. Perhaps:
LEFT OUTER JOIN dbo.MMP_PPRVU as MMP_PPRVU_1
ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
So it doesn't conflict with:
LEFT OUTER JOIN MMP_PPRVU
ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
I find it helpful to use slightly different indentation when writing my FROM clause so that these things stand out a bit more and it's easier to parse.
FROM CLARITY.dbo.CLARITY_TDL_TRAN AS CLARITY_TDL_TRAN
LEFT OUTER JOIN dbo.MMP_STAGING_UNITS AS MMP_STAGING_UNITS
ON MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID
AND CLARITY_TDL_TRAN.DETAIL_TYPE IN (
1,
10
)
LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP
ON CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1
ON CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
LEFT OUTER JOIN CLARITY.dbo.CLARITY_POS AS CLARITY_POS
ON CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_DEP AS CLARITY_DEP
ON CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_LOC AS CLARITY_LOC
ON CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
LEFT OUTER JOIN dbo.mmp_PatientsTestaYes AS mmp_PatientsTestaYes
ON CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_SA AS CLARITY_SA
ON CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_SER AS CLARITY_SER
ON CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAF_TIN AS CLARITY_EAF_TIN
ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
LEFT OUTER JOIN dbo.MMP_PPRVU AS MMP_PPRVU_1
ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
LEFT OUTER JOIN dbo.MMP_revctrs$ AS MMP_revctrs$
ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
LEFT OUTER JOIN MMP_PPRVU
ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
I like to use indentation to help keep joins understandable.
You can see below that you give the second join against CLARITY_EAP the alias of CLARITY_EAP1 but then when you try to join against it you use the alias CLARITY_EAP_1 which isn't defined and is likely throwing an error.
SELECT........
FROM CLARITY.dbo.CLARITY_TDL_TRAN as CLARITY_TDL_TRAN
left outer join dbo.MMP_STAGING_UNITS as MMP_STAGING_UNITS on MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID and CLARITY_TDL_TRAN.DETAIL_TYPE in (1, 10)
left outer join dbo.MMP_PPRVU as MMP_PPRVU1 on MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
left outer join CLARITY.dbo.CLARITY_EAP as CLARITY_EAP on CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
left outer join dbo.MMP_revctrs$ as MMP_revctrs$ ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
left outer join CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1 on CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
left outer join MMP_PPRVU as MMP_PPRVU2 ON CLARITY_EAP1.PROC_CODE = MMP_PPRVU2.HCPCS
left outer join CLARITY.dbo.CLARITY_POS as CLARITY_POS on CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
left outer join CLARITY.dbo.CLARITY_DEP as CLARITY_DEP on CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
left outer join CLARITY.dbo.CLARITY_LOC as CLARITY_LOC on CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
left outer join CLARITY.dbo.CLARITY_EAF_TIN as CLARITY_EAF_TIN ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
left outer join dbo.mmp_PatientsTestaYes as mmp_PatientsTestaYes on CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
left outer join CLARITY.dbo.CLARITY_SA as CLARITY_SA on CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
left outer join CLARITY.dbo.CLARITY_SER as CLARITY_SER on CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
WHERE
CLARITY_TDL_TRAN.DETAIL_TYPE IN (1, 10) AND
mmp_PatientsTestaYes.PAT_ID IS NULL AND
CLARITY_EAF_TIN.LINE = 1

Inner join more than 2 tables in a single database

i want to innerjoin more than 3 tables in one select query in one database all of them are having rm_id as a common column but i want to display the rows of all tables in one sql query is it possible if it is then i would hearly request u to provide some code
select * from
bk_det inner join
bk_rep inner join
bk_sec inner join
mut_det inner join
rm_det inner join
soil_det
on
bk_det.rm_id = bk_rep.rm_id = bk_sec.rm_id = mut_det.rm_id = rm_det.rm_id = soil_det.rm_id
You need an on clause for each inner join. e.g.
select * from a
inner join b on a.id = b.id
inner join c on b.id = c.id
You are missing the ON clause for each join:
select *
from bk_det
inner join bk_rep
on bk_det.rm_id = bk_rep.rm_id
inner join bk_sec
on bk_rep.rm_id = bk_sec.rm_id
inner join mut_det
on bk_sec.rm_id = mut_det.rm_id
inner join rm_det
on mut_det.rm_id = rm_det.rm_id
inner join soil_det
on rm_det.rm_id = soil_det.rm_id
Note: you will have to check the join condition column names since I do not know your table structure
If you need help with learning join syntax, here is a great visual explanation of joins.
Since you are using an INNER JOIN this will return records if the rm_id exists in each table.
You might need to use a LEFT JOIN:
select *
from bk_det
left join bk_rep
on bk_det.rm_id = bk_rep.rm_id
left join bk_sec
on bk_rep.rm_id = bk_sec.rm_id
left join mut_det
on bk_sec.rm_id = mut_det.rm_id
left join rm_det
on mut_det.rm_id = rm_det.rm_id
left join soil_det
on rm_det.rm_id = soil_det.rm_id

SQL joining 6 tables

Please take a look at the image.There are 5tables related to appointTable with appointID.
Now i need all the data with same appointment id..what should be the joining query?Can any one help me about it?
Here is the generated query(I am using Left outer Join)
SELECT dbo.Appointment.appointment_id, dbo.Appointment.patient_id, dbo.PatientInvestigaiton.investigation_name, dbo.PatientInvestigaiton.investigation_id,
dbo.PatientTreatmentMedicine.medecine_id, dbo.PatientTreatmentMedicine.medicinename, dbo.PatientTreatmentMedicine.medicinetype,
dbo.PatientFindings.finding_id, dbo.PatientFindings.finding_value, dbo.PatientAdvice.advice_description, dbo.PatientCC.cc_value, dbo.PatientCC.cc_id,
dbo.PatientDiagonosis.diagonosis_name, dbo.PatientDiagonosis.diagonosis_id
FROM dbo.Appointment LEFT OUTER JOIN
dbo.PatientInvestigaiton ON dbo.Appointment.appointment_id = dbo.PatientInvestigaiton.appointment_id LEFT OUTER JOIN
dbo.PatientTreatmentMedicine ON dbo.Appointment.appointment_id = dbo.PatientTreatmentMedicine.appointment_id LEFT OUTER JOIN
dbo.PatientFindings ON dbo.Appointment.appointment_id = dbo.PatientFindings.appointment_id LEFT OUTER JOIN
dbo.PatientDiagonosis ON dbo.Appointment.appointment_id = dbo.PatientDiagonosis.appointment_id LEFT OUTER JOIN
dbo.PatientCC ON dbo.Appointment.appointment_id = dbo.PatientCC.appointment_id LEFT OUTER JOIN
dbo.PatientAdvice ON dbo.Appointment.appointment_id = dbo.PatientAdvice.appointment_id
where dbo.Appointment.appointment_id='46';
Since appointmnent_id is the primary key of Appointment, this table has a 1:N relationship with all 6 tables.
This is the case where joining to these 6 tables will produce multiple rows with duplicate data, it's like a Cartesian Product. For example if (for only one id=46), there are:
3 rows for PatientInvestigation
6 rows for PatientTreatmentMedicine
4 rows for PatientFindings
2 rows for PatientDiagnosis
2 rows for PatientCC
5 rows for PatientAdvice
you'll get 3x6x4x2x2x5 = 1440 rows in the result set, while you only need 3+6+4+2+2+5 (+1) = 23 rows. That is 60 times more rows (and with many more columns) than needed.
It's better if you do 6 separate queries with one JOIN to one (of the 6) tables in each query (and one more query to get the data from the base table Appointment). And combine the results of the 6 queries in the application code. Example for the base query and the query to join to the first table:
Base table:
SELECT
a.appointment_id,
a.patient_id
FROM
Appointment AS a
WHERE
a.appointment_id = 46
Join-1 to PatientInvestigation:
SELECT
pi.investigation_name,
pi.investigation_id
FROM
Appointment AS a
JOIN
PatientInvestigation AS pi
ON pi.appointment_id = a.appointment_id
WHERE
a.appointment_id = 46
SELECT
Appointment.appointment_id,
Appointment.patient_id,
PatientInvestigaiton.investigation_name,
PatientInvestigaiton.investigation_id,
PatientTreatmentMedicine.medecine_id,
PatientTreatmentMedicine.medicinename,
PatientTreatmentMedicine.medicinetype,
PatientFindings.finding_id,
PatientFindings.finding_value,
PatientAdvice.advice_description,
PatientCC.cc_value,
PatientCC.cc_id,
PatientDiagonosis.diagonosis_name,
PatientDiagonosis.diagonosis_id
FROM
Appointment
LEFT OUTER JOIN PatientInvestigaiton ON Appointment.appointment_id = PatientInvestigaiton.appointment_id AND Appointment.appointment_id='46'
LEFT OUTER JOIN PatientTreatmentMedicine ON Appointment.appointment_id = PatientTreatmentMedicine.appointment_id
LEFT OUTER JOIN PatientFindings ON Appointment.appointment_id = PatientFindings.appointment_id
LEFT OUTER JOIN PatientDiagonosis ON Appointment.appointment_id = PatientDiagonosis.appointment_id
LEFT OUTER JOIN PatientCC ON Appointment.appointment_id = PatientCC.appointment_id
LEFT OUTER JOIN PatientAdvice ON Appointment.appointment_id = PatientAdvice.appointment_id
SELECT {TABLE1}.appointment_id,{OTHER FIELDS} FROM {TABLE1}
JOIN {TABLE2} ON {TABLE1}.appointment_id = {TABLE2}.appointment_id
JOIN {TABLE3} ON {TABLE1}.appointment_id = {TABLE3}.appointment_id
JOIN {TABLE4} ON {TABLE1}.appointment_id = {TABLE4}.appointment_id
JOIN {TABLE5} ON {TABLE1}.appointment_id = {TABLE5}.appointment_id
JOIN {TABLE6} ON {TABLE1}.appointment_id = {TABLE6}.appointment_id
JOIN {TABLE7} ON {TABLE1}.appointment_id = {TABLE7}.appointment_id
JOIN {TABLE8} ON {TABLE1}.appointment_id = {TABLE8}.appointment_id;
Try this:
SELECT
dbo.Appointment.appointment_id, dbo.Appointment.patient_id,
dbo.PatientInvestigaiton.investigation_name, dbo.PatientInvestigaiton.investigation_id,
dbo.PatientTreatmentMedicine.medecine_id, dbo.PatientTreatmentMedicine.medicinename, dbo.PatientTreatmentMedicine.medicinetype,
dbo.PatientFindings.finding_id, dbo.PatientFindings.finding_value,
dbo.PatientAdvice.advice_description,
dbo.PatientCC.cc_value, dbo.PatientCC.cc_id,
dbo.PatientDiagonosis.diagonosis_name, dbo.PatientDiagonosis.diagonosis_id
FROM
dbo.Appointment
LEFT JOIN dbo.PatientInvestigaiton
ON dbo.Appointment.appointment_id = dbo.PatientInvestigaiton.appointment_id
LEFT JOIN dbo.PatientTreatmentMedicine
ON dbo.Appointment.appointment_id = dbo.PatientTreatmentMedicine.appointment_id
LEFT JOIN dbo.PatientFindings
ON dbo.Appointment.appointment_id = dbo.PatientFindings.appointment_id
LEFT JOIN dbo.PatientDiagonosis
ON dbo.Appointment.appointment_id = dbo.PatientDiagonosis.appointment_id
LEFT JOIN dbo.PatientCC
ON dbo.Appointment.appointment_id = dbo.PatientCC.appointment_id
LEFT JOIN dbo.PatientAdvice
ON dbo.Appointment.appointment_id = dbo.PatientAdvice.appointment_id
WHERE
dbo.Appointment.appointment_id='46';

Can anyone fathom a why do this or how it works correctly with this bit of SQL?

I've changed the table names but I found this FROM statement in a SP I'm optimizing and I'm wondering the how this could come into being, why would you ever do anything like this, and why does it still work.
FROM tblPezFill pf
RIGHT OUTER JOIN tblWolveLocation stfl
RIGHT OUTER JOIN tblDuckPez pp
RIGHT OUTER JOIN tblChaos o
INNER JOIN tblBillLeg bi
INNER JOIN #Facty selFac
ON bi.WolveId = selFac.WolveId
ON o.ChaosID = bi.ChaosId
LEFT OUTER JOIN vwDonald stPres
RIGHT OUTER JOIN tblStockChaos so
ON stPres.DonaldId = so.DonaldId
ON o.StockChaosID = so.StockChaosID
ON pp.PezID = o.PezID
ON stfl.WolveLocationId = so.WolveLocationId
ON pf.PezFillID = o.PezFillID
LEFT OUTER JOIN tblCheeseburger p ON bi.CheeseburgerId = p.CheeseburgerId
LEFT OUTER JOIN tblChaosCheeseburgerShipped ops ON o.ChaosID = ops.ChaosID
LEFT OUTER JOIN vwDonald pres ON pp.DonaldId = pres.DonaldId
LEFT OUTER JOIN tblDuck ON pp.DuckID = tblDuck.DuckId
LEFT OUTER JOIN vwDuck ON pp.DuckID = vwDuck.DuckId
LEFT OUTER JOIN tblWolveLocation fl ON pf.WolveLocationId = fl.WolveLocationId
I've cleaned it up to
FROM tblBillLeg bi
INNER JOIN #Facty selFac ON bi.WolveId = selFac.WolveId
LEFT OUTER JOIN tblCheeseburger p ON bi.CheeseburgerId = p.CheeseburgerId
LEFT OUTER JOIN tblChaos o ON o.ChaosID = bi.ChaosId
LEFT OUTER JOIN tblDuckPez pp ON pp.PezID = o.PezID
LEFT OUTER JOIN tblPezFill pf ON pf.PezFillID = o.PezFillID
LEFT OUTER JOIN tblChaosCheeseburgerShipped ops ON o.ChaosID = ops.ChaosID
LEFT OUTER JOIN tblStockChaos so ON o.StockChaosID = so.StockChaosID
LEFT OUTER JOIN tblWolveLocation stfl ON stfl.WolveLocationId = so.WolveLocationId
LEFT OUTER JOIN vwDonald stPres ON stPres.DonaldId = so.DonaldId
LEFT OUTER JOIN vwDonald pres ON pp.DonaldId = pres.DonaldId
LEFT OUTER JOIN tblDuck ON pp.DuckID = tblDuck.DuckId
LEFT OUTER JOIN vwDuck ON pp.DuckID = vwDuck.DuckId
LEFT OUTER JOIN tblWolveLocation fl ON pf.WolveLocationId = fl.WolveLocationId
Which I believe should be equivalent, I need some testing data. I just don't get how the first version really worked at all.
Join expressions in SQL are constructive, just like arithmetic expressions.
That is, you can do the following:
A JOIN B ON <expr-AB>
But you can also substitute another join expression for B:
A JOIN (B JOIN C ON <expr-BC>) ON <expr-AB>
And the parens may be superfluous if the natural order of precedence would give the same result:
A JOIN B JOIN C ON <expr-BC> ON <expr-AB>
So the huge fugly query you showed is legal syntax, but it certainly could be more clear.
I see the query as a hierarchy like this:
so
so --left--> stfl
so --left--> stPres
so --left--> o
o --inner-> bi
bi --inner-> selFac
bi --left--> p
o --left--> pp
pp --left--> pres
pp --left--> tblDuck
pp --left--> vwDuck
o --left--> pf
pf --left--> fl
o --left--> ops
I would rewrite the query like this:
FROM tblStockChaos so
LEFT OUTER JOIN tblWolveLocation stfl
ON so.WolveLocationId = stfl.WolveLocationId
LEFT OUTER JOIN vwDonald stPres
ON so.DonaldId = stPres.DonaldId
LEFT OUTER JOIN
(tblChaos o
INNER JOIN
(tblBillLeg bi
INNER JOIN #Facty selFac
ON bi.WolveId = selFac.WolveId
LEFT OUTER JOIN tblCheeseburger p
ON bi.CheeseburgerId = p.CheeseburgerId)
ON o.ChaosID = bi.ChaosId
LEFT OUTER JOIN
(tblDuckPez pp
LEFT OUTER JOIN vwDonald pres
ON pp.DonaldId = pres.DonaldId
LEFT OUTER JOIN tblDuck
ON pp.DuckID = tblDuck.DuckId
LEFT OUTER JOIN vwDuck
ON pp.DuckID = vwDuck.DuckId)
ON o.PezID = pp.PezID
LEFT OUTER JOIN tblPezFill
(tblPezFill pf
LEFT OUTER JOIN tblWolveLocation fl
ON pf.WolveLocationId = fl.WolveLocationId)
ON o.PezFillID = pf.PezFillID
LEFT OUTER JOIN tblChaosCheeseburgerShipped ops
ON o.ChaosID = ops.ChaosID
)
I can't guarantee I've got all the conditions perfect. But that's the idea.
My approach was to generate a query plan using table variables and then attempt to re-write the query in the manner it would be compiled.
If you setup with this code:
DECLARE #tblPezFill TABLE (PezFillID INT, WolveLocationID INT) -- pf / f1
DECLARE #tblWolveLocation TABLE (WolveLocationID INT) -- stfl
DECLARE #tblDuckPez TABLE (Pezid INT, DonaldID INT, DuckID INT) -- pp
DECLARE #tblChaos TABLE (StockChaosID INT, ChaosID INT, PezID INT,
PezFillID INT) -- o
DECLARE #tblBillLeg TABLE (WolveID INT, ChaosID INT,
CheeseburgerID INT) -- bi
DECLARE #Facty TABLE (WolveId INT) -- selFac
DECLARE #vwDonald TABLE (DonaldID INT) -- stPres
DECLARE #tblStockChaos TABLE (StockChaosID INT, DonaldID INT,
WolveLocationID INT) -- so
DECLARE #tblCheeseburger TABLE (CheeseburgerID INT) -- p
DECLARE #tblChaosCheeseburgerShipped TABLE (ChaosID INT) -- ops
DECLARE #tblDuck TABLE (DuckID INT) -- tblDuck
DECLARE #vwDuck TABLE (DuckID INT) -- vwDuck
And then run the query:
SELECT *
FROM #tblPezFill pf
RIGHT OUTER JOIN #tblWolveLocation stfl
RIGHT OUTER JOIN #tblDuckPez pp
RIGHT OUTER JOIN #tblChaos o
INNER JOIN #tblBillLeg bi
INNER JOIN #Facty selFac
ON bi.WolveId = selFac.WolveId
ON o.ChaosID = bi.ChaosId
LEFT OUTER JOIN #vwDonald stPres
RIGHT OUTER JOIN #tblStockChaos so
ON stPres.DonaldId = so.DonaldId
ON o.StockChaosID = so.StockChaosID
ON pp.PezID = o.PezID
ON stfl.WolveLocationId = so.WolveLocationId
ON pf.PezFillID = o.PezFillID
LEFT OUTER JOIN #tblCheeseburger p ON bi.CheeseburgerId = p.CheeseburgerId
LEFT OUTER JOIN #tblChaosCheeseburgerShipped ops ON o.ChaosID = ops.ChaosID
LEFT OUTER JOIN #vwDonald pres ON pp.DonaldId = pres.DonaldId
LEFT OUTER JOIN #tblDuck tblDuck ON pp.DuckID = tblDuck.DuckId
LEFT OUTER JOIN #vwDuck vwDuck ON pp.DuckID = vwDuck.DuckId
LEFT OUTER JOIN #tblWolveLocation fl ON pf.WolveLocationId = fl.WolveLocationId
The beginning of the query plan looks like this:
The remainder is just a series of left joins so I left them out. My thinking is that if you can duplicate the query plan, you'll know that your new query exactly matches the logic of the original.
Here is what I came up with:
SELECT *
FROM (
SELECT
a2.WolveId,
a2.ChaosID,
a2.CheeseburgerID,
a2.PezFillID,
a2.PezID,
a2.StockChaosID,
a3.DonaldID,
a3.WolveLocationID
FROM (
SELECT
selFac.WolveId,
a1.ChaosID,
a1.CheeseburgerID,
a1.PezFillID,
a1.PezID,
a1.StockChaosID
FROM #Facty selFac
INNER JOIN (
SELECT
bi.ChaosID,
bi.CheeseburgerID,
bi.WolveID,
o.PezFillID,
o.PezID,
o.StockChaosID
FROM #tblBillLeg bi
INNER JOIN #tblChaos o
ON bi.ChaosID = o.ChaosID
) a1
ON selFac.WolveId = a1.WolveID
) a2
LEFT OUTER JOIN (
SELECT
stPres.DonaldID,
so.StockChaosID,
so.WolveLocationID
FROM #vwDonald stPres
LEFT OUTER JOIN #tblStockChaos so
ON stPres.DonaldID = so.DonaldID
) a3
ON a2.StockChaosID = a3.StockChaosID
) a4
LEFT OUTER JOIN #tblDuckPez pp
ON a4.PezID = pp.Pezid
LEFT OUTER JOIN #tblCheeseburger p
ON a4.CheeseburgerID = p.CheeseburgerID
LEFT OUTER JOIN #tblChaosCheeseburgerShipped ops
ON a4.ChaosID = ops.ChaosID
LEFT OUTER JOIN #vwDonald pres
ON a4.DonaldID = pres.DonaldID
LEFT OUTER JOIN #tblDuck tblDuck
ON pp.DuckID = tblDuck.DuckID
LEFT OUTER JOIN #tblWolveLocation fl
ON a4.WolveLocationID = fl.WolveLocationID
The only difference in my execution plan is that a couple fields had an additional step of compute scalar and that the final set of left outer joins were not in the exact same order as the original.