如何解决这个SQL查询(标题和细节)?

srh*_*srh 6 sql sql-server sql-server-2008

我正在使用SQL Server 2008 R2.

我有一个与标题和详细信息表相关的SQL查询问题.我有一个标题表,我在那里存储location&department&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 )
Run Code Online (Sandbox Code Playgroud)

我有一个详细信息表:

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 )
Run Code Online (Sandbox Code Playgroud)

header表具有唯一键作为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'

标头表中的每一行都可以在详细信息表中有多行.

detail表具有唯一键,如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',hours = 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',hours = 3.00

SQL查询应该返回1000598 employee_id,因为1000598具有相同week_start_date ='2016-02-08'的department_code = 33和department_code = 39的数据.

SQL查询应返回1000599 employee_id,因为1000599具有相同week_start_date ='2016-02-08'的location_code = 22和location_code = 21的数据.

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  
Run Code Online (Sandbox Code Playgroud)

不多.

Fuz*_*ree 3

我想找到那些位于不同标题、具有相同 week_start_date 但位置和/或部门不同的员工。

下面的查询将返回所有(employee_id, week_start_date)具有超过 1location_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
)    
Run Code Online (Sandbox Code Playgroud)