SQL Server 2008 R2를 사용하고 있습니다.
헤더 및 세부 정보 테이블과 관련된 SQL 쿼리 질문이 있습니다. 위치 및 부서 및 week_start_date를 저장하는 헤더 테이블이 있습니다. employee_id & job_code & work_date & hours를 저장하는 세부 테이블이 있습니다.
week_start_date는 같지만 위치 및 / 또는 부서가 다른 헤더에있는 직원을 찾고 싶습니다.
설명은 다음과 같습니다.
나는이 헤더 테이블 :
CREATE TABLE header (
header_id bigint not null PRIMARY KEY,
location_code int not null,
department_code int not null,
week_start_date datetime not null )
나는이 세부 사항 테이블 :
CREATE TABLE detail (
detail_id bigint not null PRIMARY KEY,
header_id bigint not null FOREIGN KEY header(header_id),
employee_id int not null,
job_code int not null,
work_date datetime not null,
hours decimal(8,2) not null )
헤더 테이블에는 location_code + department_code + week_start_date와 같은 고유 키가 있습니다.
예를 들어 다음은 헤더 테이블 의 데이터입니다 .
header_id = 11, location_code = 22, department_code = 33, week_start_date = '2016-02-08'
header_id = 12, location_code = 22, department_code = 39, week_start_date = '2016-02-08'
header_id = 13, location_code = 22, department_code = 33, week_start_date = '2016-02-15'
header_id = 14, location_code = 21, department_code = 33, week_start_date = '2016-02-08'
헤더 테이블의 각 행에는 세부 정보 테이블에 여러 행이있을 수 있습니다 .
상세 테이블에는 header_id + employee_id + job_code + work_date와 같은 고유 키가 있습니다.
예를 들어 다음은 1000598 employee_id에 대한 세부 테이블 의 데이터입니다 .
detail_id = 101, header_id = 11, employee_id = 1000598, job_code = 77, work_date = '2016-02-08', 시간 = 5.00
detail_id = 102, header_id = 11, employee_id = 1000598, job_code = 77, work_date = '2016-02-09', 시간 = 4.00
detail_id = 109, header_id = 12, employee_id = 1000598, job_code = 79, work_date = '2016-02-11', 시간 = 4.50
예를 들어 다음은 1000599 employee_id에 대한 세부 테이블 의 데이터입니다 .
detail_id = 121, header_id = 11, employee_id = 1000599, job_code = 78, work_date = '2016-02-10', 시간 = 8.00
detail_id = 122, header_id = 14, employee_id = 1000599, job_code = 75, work_date = '2016-02-12', 시간 = 3.00
예를 들어 다음은 1000600 employee_id에 대한 세부 테이블 의 데이터입니다 .
detail_id = 131, header_id = 11, employee_id = 1000600, job_code = 72, work_date = '2016-02-11', 시간 = 7.00
detail_id = 132, header_id = 13, employee_id = 1000600, job_code = 75, work_date = '2016-02-17', 시간 = 3.00
1000598에는 동일한 week_start_date = '2016-02-08'에 대한 department_code = 33 및 department_code = 39에 대한 데이터가 모두 있으므로 SQL 쿼리는 1000598 employee_id를 반환해야합니다.
1000599에 동일한 week_start_date = '2016-02-08'에 대한 location_code = 22 및 location_code = 21에 대한 데이터가 모두 있으므로 SQL 쿼리는 1000599 employee_id를 반환해야합니다.
SQL 쿼리는 1000600 employee_id를 반환하지 않아야합니다.
이것이 내가 생각 해낸 시작입니다.
select
h.employee_id,
d.week_start_date
from
header h (nolock)
inner join detail d (nolock)
on h.header_id = d.header_id
group by
h.employee_id,
d.week_start_date
order by
1,
2
별로.
week_start_date는 같지만 위치 및 / 또는 부서가 다른 헤더에있는 직원을 찾고 싶습니다.
아래 쿼리 (employee_id, week_start_date)
는 1 개 이상 location_code
또는department_code
select d.employee_id, h.week_start_date
from detail d
join header h on h.header_id = d.header_id
group by d.employee_id, h.week_start_date -- employees with same week_start_date
having (
count(distinct h.location_code) > 1 -- have more than 1 location
or count(distinct h.department_code) > 1 -- or more than 1 department
)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다