내가 SSMS 2008 R2를 사용하고하고 TSQL 쿼리를 개발하고있다. 나는 단지 1 레코드/profile_name을 원한다. 이러한 값 중 일부는 NULL이기 때문에 현재 대부분의 테이블에서 LEFT JOINS를 수행하고 있습니다. 하지만 LEFT JOIN의 문제점은 이제 일부 profile_names에 대해> 1 레코드를 얻는다는 것입니다.
그러나 이것을 INNER JOINs로 변경하면 일부 profile_names는이 열에 대해 NULL 값을 가지기 때문에 완전히 제외됩니다. NULL 값과 상관없이 조회 결과를 하나의 레코드/profile_name으로 어떻게 제한합니까? NULL이 아닌 값이있는 경우 NULL이 아닌 값으로 레코드를 선택해야합니다.
select distinct
gp.group_profile_id,
gp.profile_name,
gp.license_number,
gp.is_accepting,
case when gp.is_accepting = 1 then 'Yes'
when gp.is_accepting = 0 then 'No '
end as is_accepting_placement,
mo.profile_name as managing_office,
regions.[region_description] as region,
pv.vendor_name,
pv.id as vendor_id,
at.description as applicant_type,
dbo.GetGroupAddress(gp.group_profile_id, null, 0) as [Office Address],
gsv.status_description
from group_profile gp With (NoLock)
inner join group_profile_type gpt With (NoLock) on gp.group_profile_type_id = gpt.group_profile_type_id and gpt.type_code = 'FOSTERHOME' and gp.agency_id = @agency_id and gp.is_deleted = 0
inner join group_profile mo With (NoLock) on gp.managing_office_id = mo.group_profile_id
left outer join payor_vendor pv With (NoLock) on gp.payor_vendor_id = pv.payor_vendor_id
left outer join applicant_type at With (NoLock) on gp.applicant_type_id = at.applicant_type_id and at.is_foster_home = 1
inner join group_status_view gsv With (NoLock) on gp.group_profile_id = gsv.group_profile_id and gsv.status_value = 'OPEN' and gsv.effective_date =
(Select max(b.effective_date) from group_status_view b With (NoLock)
where gp.group_profile_id = b.group_profile_id)
left outer join regions With (NoLock) on isnull(mo.regions_id, gp.regions_id) = regions.regions_id
left join enrollment en on en.group_profile_id = gp.group_profile_id
join event_log el on el.event_log_id = en.event_log_id
left join people client on client.people_id = el.people_id
당신이 볼 수 있듯이, 위의 쿼리의 결과가 1 행/PROFILE_NAME입니다 :
group_profile_id profile_name license_number is_accepting is_accepting_placement managing_office region vendor_name vendor_id applicant_type Office Address status_description Cert Date2
하지만 지금은이 왼쪽에 추가 할 때 발생하는보고 조인 한 다음 초기 쿼리입니다 추가 열 : 같은 profile_names의 모두
select distinct
gp.group_profile_id,
gp.profile_name,
gp.license_number,
gp.is_accepting,
case when gp.is_accepting = 1 then 'Yes'
when gp.is_accepting = 0 then 'No '
end as is_accepting_placement,
mo.profile_name as managing_office,
regions.[region_description] as region,
pv.vendor_name,
pv.id as vendor_id,
at.description as applicant_type,
dbo.GetGroupAddress(gp.group_profile_id, null, 0) as [Office Address],
gsv.status_description,
ri.[description] as race
from group_profile gp With (NoLock)
inner join group_profile_type gpt With (NoLock) on gp.group_profile_type_id = gpt.group_profile_type_id and gpt.type_code = 'FOSTERHOME' and gp.agency_id = @agency_id and gp.is_deleted = 0
inner join group_profile mo With (NoLock) on gp.managing_office_id = mo.group_profile_id
left outer join payor_vendor pv With (NoLock) on gp.payor_vendor_id = pv.payor_vendor_id
left outer join applicant_type at With (NoLock) on gp.applicant_type_id = at.applicant_type_id and at.is_foster_home = 1
inner join group_status_view gsv With (NoLock) on gp.group_profile_id = gsv.group_profile_id and gsv.status_value = 'OPEN' and gsv.effective_date =
(Select max(b.effective_date) from group_status_view b With (NoLock)
where gp.group_profile_id = b.group_profile_id)
left outer join regions With (NoLock) on isnull(mo.regions_id, gp.regions_id) = regions.regions_id
left join enrollment en on en.group_profile_id = gp.group_profile_id
join event_log el on el.event_log_id = en.event_log_id
left join people client on client.people_id = el.people_id
left join race With (NoLock) on el.people_id = race.people_id
left join race_info ri with (nolock) on ri.race_info_id = race.race_info_id
위의 쿼리 결과 만 NULL 경주 값 일부 :
group_profile_id profile_name license_number is_accepting is_accepting_placement managing_office region vendor_name vendor_id applicant_type Office Address status_description Cert Date2 race
불행히도이 하나의 추가 필드 값 (경주)에 대해 2 개의 추가 테이블에 참여해야한다는 것은 복잡합니다. 위의 마지막 두 LEFT JOIN을 INNER JOIN으로 변경하면 위의 NULL 행이 제거됩니다. 그러나 일부 프로필 이름도 제거합니다.
group_profile_id profile_name license_number is_accepting is_accepting_placement managing_office region vendor_name vendor_id applicant_type Office Address status_description Cert Date2 race
이 질문에 필요한 모든 세부 정보를 제공해 드리고자합니다. 작동
이것은 너무 읽기 쉽습니다. 표 데이터의 형식이 올바르지 않습니다. – codingbiz
방금 서식을 수정했습니다. – salvationishere
JOIN 된 테이블에 둘 이상의 레코드가있는 경우 여러 행이 발생할 수 있습니다. 마지막 2 개 조인에서 하나씩 다른 조인을 제거하고 중복을 일으키는 조인을 확인하십시오. match 또는 raceinfo.race_info_id가 하나 이상인 race.people_id가있을 수 있습니다. – codingbiz