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';
Related
When i run this query in window system behave correctly UNNSET
but when i run this query Linux behave different.unnset duplicate record list on different row
SELECT DISTINCT
"billing_billmanagement"."creation_date",
"billing_billmanagement"."bill_number",
unnest(array_agg(DISTINCT "inventory_product"."product_name")) AS "product",
unnest(array_agg(DISTINCT "services_service"."name")) AS "service"
FROM "billing_billmanagement"
INNER JOIN "users_staffuser" ON ("billing_billmanagement"."staff_id" = "users_staffuser"."id")
INNER JOIN "auth_user" ON ("users_staffuser"."user_id" = "auth_user"."id")
LEFT OUTER JOIN "billing_customerproductbill" ON ("billing_billmanagement"."id" = "billing_customerproductbill"."bill_id")
LEFT OUTER JOIN "inventory_product" ON ("billing_customerproductbill"."product_id" = "inventory_product"."id")
LEFT OUTER JOIN "billing_customerservicebill" ON ("billing_billmanagement"."id" = "billing_customerservicebill"."bill_id")
LEFT OUTER JOIN "services_service" ON ("billing_customerservicebill"."service_id" = "services_service"."id")
WHERE "billing_billmanagement"."creation_date" BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY billing_billmanagement.creation_date,
billing_billmanagement.bill_number
ORDER BY "billing_billmanagement"."creation_date" ASC
If getting duplicate rows is the problem, try this
SELECT billing_billmanagement.creation_date,
billing_billmanagement.bill_number,
inventory_product.product_name AS product,
services_service.name AS service
FROM billing_billmanagement
INNER JOIN users_staffuser ON (billing_billmanagement.staff_id = users_staffuser.id)
INNER JOIN auth_user ON (users_staffuser.user_id = auth_user.id)
LEFT OUTER JOIN billing_customerproductbill ON (billing_billmanagement.id = billing_customerproductbill.bill_id)
LEFT OUTER JOIN inventory_product ON (billing_customerproductbill.product_id = inventory_product.id)
LEFT OUTER JOIN billing_customerservicebill ON (billing_billmanagement.id = billing_customerservicebill.bill_id)
LEFT OUTER JOIN services_service ON (billing_customerservicebill.service_id = services_service.id)
WHERE billing_billmanagement.creation_date BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY 1,
2,
3,
4
ORDER BY 1 ASC;
When i run this query in window system behave correctly UNNSET
but when i run this query Linux behave different.unnset duplicate record list on different row
SELECT DISTINCT
"billing_billmanagement"."creation_date",
"billing_billmanagement"."bill_number",
unnest(array_agg(DISTINCT "inventory_product"."product_name")) AS "product",
unnest(array_agg(DISTINCT "services_service"."name")) AS "service"
FROM "billing_billmanagement"
INNER JOIN "users_staffuser" ON ("billing_billmanagement"."staff_id" = "users_staffuser"."id")
INNER JOIN "auth_user" ON ("users_staffuser"."user_id" = "auth_user"."id")
LEFT OUTER JOIN "billing_customerproductbill" ON ("billing_billmanagement"."id" = "billing_customerproductbill"."bill_id")
LEFT OUTER JOIN "inventory_product" ON ("billing_customerproductbill"."product_id" = "inventory_product"."id")
LEFT OUTER JOIN "billing_customerservicebill" ON ("billing_billmanagement"."id" = "billing_customerservicebill"."bill_id")
LEFT OUTER JOIN "services_service" ON ("billing_customerservicebill"."service_id" = "services_service"."id")
WHERE "billing_billmanagement"."creation_date" BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY billing_billmanagement.creation_date,
billing_billmanagement.bill_number
ORDER BY "billing_billmanagement"."creation_date" ASC
If getting duplicate rows is the problem, try this
SELECT billing_billmanagement.creation_date,
billing_billmanagement.bill_number,
inventory_product.product_name AS product,
services_service.name AS service
FROM billing_billmanagement
INNER JOIN users_staffuser ON (billing_billmanagement.staff_id = users_staffuser.id)
INNER JOIN auth_user ON (users_staffuser.user_id = auth_user.id)
LEFT OUTER JOIN billing_customerproductbill ON (billing_billmanagement.id = billing_customerproductbill.bill_id)
LEFT OUTER JOIN inventory_product ON (billing_customerproductbill.product_id = inventory_product.id)
LEFT OUTER JOIN billing_customerservicebill ON (billing_billmanagement.id = billing_customerservicebill.bill_id)
LEFT OUTER JOIN services_service ON (billing_customerservicebill.service_id = services_service.id)
WHERE billing_billmanagement.creation_date BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY 1,
2,
3,
4
ORDER BY 1 ASC;
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
I am a SQL Server beginner and so I have been using MS Access to create queries and then have been amending them for SQL Server.
I have created a query which includes a left join, which works perfectly in Access but when I copy the SQL code across to SQL Server it does not show the null values and I cannot work out why.
I want to show all of the attribute values whether they include a value or not. Can anyone help? The code is as follows:
SELECT DISTINCT
tbLease.LeaseTitle
, tbAttributeValue.AttributeTemplateDefinitionLinkID
, tbAttributeValue.Value
FROM
((((tbBusinessUnit
LEFT JOIN
tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID)
INNER JOIN
tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID = tbBuildingLinkBusinessUnit.BusinessUnitID)
INNER JOIN
tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID)
INNER JOIN
(tbUnitLocation
INNER JOIN
tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID)
ON tbBuilding.BuildingID = tbUnitLocation.LocationID)
INNER JOIN
tbLease ON tbUnit.UnitID = tbLease.UnitID
WHERE
(((tbAttributeValue.AttributeTemplateDefinitionLinkID) = 30
Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 31
Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 32));
Hard to say what you want without some examples but you might want this:
WHERE coalesce(tbAttributeValue.AttributeTemplateDefinitionLinkID,30) in (30,31,32);
This will give you items where AttributeTemplateDefinitionLinkID is null
(that is it did not join on the left join) OR is one of those 3 values.
Right now if you don't join with the left join it will not display that row because of the where condition, so your left join is the same as an inner join.
The NULL values you would normally see with a left join are getting filtered out by your WHERE clause.
Any time you add filters to a WHERE clause that apply to an outer joined table, it will effectively make it the same as an inner join unless you include NULL values as an option in your where clause as well.
SELECT DISTINCT
tbLease.LeaseTitle,
tbAttributeValue.AttributeTemplateDefinitionLinkID,
tbAttributeValue.Value
FROM
tbBusinessUnit
LEFT JOIN tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID
INNER JOIN tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID = tbBuildingLinkBusinessUnit.BusinessUnitID
INNER JOIN tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID
INNER JOIN tbUnitLocation ON tbBuilding.BuildingID = tbUnitLocation.LocationID
INNER JOIN tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID
INNER JOIN tbLease ON tbUnit.UnitID = tbLease.UnitID
WHERE
tbAttributeValue.AttributeTemplateDefinitionLinkID in (30, 31, 32)
or tbAttributeValue.AttributeTemplateDefinitionLinkID is null
Move the right table filters from where clause to ON condition when you are using Left Outer Join else Left join will be implicitly converted to INNER JOIN. Try this
SELECT DISTINCT tblease.leasetitle,
tbattributevalue.attributetemplatedefinitionlinkid,
tbattributevalue.value
FROM tbbusinessunit
LEFT JOIN tbattributevalue
ON tbbusinessunit.businessunitid = tbattributevalue.parentid
AND ( tbattributevalue.attributetemplatedefinitionlinkid IN
( 30, 31, 32 )
OR tbattributevalue.attributetemplatedefinitionlinkid IS
NULL )
INNER JOIN tbbuildinglinkbusinessunit
ON tbbusinessunit.businessunitid =
tbbuildinglinkbusinessunit.businessunitid
INNER JOIN tbbuilding
ON tbbuildinglinkbusinessunit.buildingid = tbbuilding.buildingid
INNER JOIN tbunitlocation
ON tbbuilding.buildingid = tbunitlocation.locationid
INNER JOIN tbunit
ON tbunitlocation.unitid = tbunit.unitid
INNER JOIN tblease
ON tbunit.unitid = tblease.unitid
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.