按作业SQL的共同名称筛选数据

时间:2015-09-23 17:42:42

标签: sql sql-server sql-server-2012

大家好,我目前正在处理一个问题,我想知道是否有办法做到这一点

工作人员希望看到一份报告,显示她在所有工作中所拥有的工作小时数,以及所有其他从事这些工作的工作人员的工作时数。因此,如果我的数据如下所示:

Client-----     Job------   Staff------     Hours    
A-----------    1 --------  Caty----------  15    
A-----------    1 --------  John----------  10    
A-----------    1 --------  Greg----------  6     
B-----------    2 --------  Caty----------  8    
B-----------    2 --------  Ralph-------    10    
B-----------    2 --------  Derek-------    12     
C-----------    1 --------  Steve -------   9      
C-----------    1 --------  Bill----------  20     
C-----------    1 --------  Mike--------    18     

对于凯蒂来说,报告应该给我这个:

Client-----     Job------   Staff------     Hours    
A-----------    1 --------  Caty----------  15    
A-----------    1 --------  John----------  10    
A-----------    1 --------  Greg----------  6     
B-----------    2 --------  Caty----------  8    
B-----------    2 --------  Ralph-------    10    
B-----------    2 --------  Derek-------    12

我不能把WHERE子句“WHERE Staff ='Caty'”放进去,因为那会过滤掉John,Greg,Ralph和Derek,只剩下Caty的时间。

select clientcode, clientname, job_name, staffcode, sum(wiphours) as Hours
from tbltranwip w
inner join tblengagement e on e.ContIndex=w.ContIndex
inner join tblJob_Header h on h.Job_Idx=w.ServPeriod
inner join tblstaff s on s.StaffIndex=w.StaffIndex
where wipdate between 'jul 1 2015' and 'jul 31 2015' and TransTypeIndex=1 
and w.ContIndex<900000 and staffcode = 'Caty'
group by clientcode, clientname, job_name, staffcode

那段代码只显示我的Caty:S

4 个答案:

答案 0 :(得分:1)

假设您的表格或视图如下所示:

CREATE TABLE job(
    Client char(1) NOT NULL,
    Job int NOT NULL,
    Staff varchar(20) NOT NULL,
    Hours int NOT NULL
);

insert into job values
('A',1, 'Caty', 15),
('A',1, 'John', 10),
('A',1, 'Greg', 6),
('B',2, 'Caty', 8),
('B',2, 'Ralph', 10),
('B',2, 'Derek', 12),
('C',1, 'Steve', 9),
('C',1, 'Bill', 20),
('C',1, 'Mike', 18);

您可以编写类似的查询来检索所需信息:

select client, job, staff, sum(hours) as totalhours
from job x
where exists (
    select 1 
    from job 
    where staff = 'Caty'
    and client = x.client
    and job = x.job)
group by client, job, staff

结果:

client job         staff                totalhours
------ ----------- -------------------- -----------
A      1           Caty                 15
A      1           Greg                 6
A      1           John                 10
B      2           Caty                 8
B      2           Derek                12
B      2           Ralph                10

SQLFiddle示例:http://sqlfiddle.com/#!6/88075/1

可能会尝试此操作来解决您的问题

with data as (
    select clientcode, clientname, job_name, staffcode, sum(wiphours) as Hours
    from tbltranwip w
    inner join tblengagement e on e.ContIndex=w.ContIndex
    inner join tblJob_Header h on h.Job_Idx=w.ServPeriod
    inner join tblstaff s on s.StaffIndex=w.StaffIndex
    where wipdate between 'jul 1 2015' and 'jul 31 2015' and TransTypeIndex=1 
    and w.ContIndex<900000
    group by clientcode, clientname, job_name, staffcode
)

select clientcode, job_name, staffcode, sum(wiphours) as Hours
from data x
where exists (
    select 1 
    from data
    where staffcode = 'Caty'
    and clientcode= x.clientcode
    and job_name= x.job_name)
group by clientcode, job_name, staffcode

答案 1 :(得分:0)

您可以在子查询中选择caty工作的作业,您可以加入获取结果

Select sum(hours) from timetable t
inner join (select client, job from timetable where staff = 'caty') as sub 
on t.client = sub.client and t.job = sub.job

这应该让你入门

答案 2 :(得分:0)

为了让与Caty工作相同的工作人员,您希望将in谓词与子查询一起使用;

之类的东西
where ...
and job in (select job from appropriate_table where Staff='Caty')

我不确定您的表格如何相关,因此您需要根据需要调整代码,但它应该会给您一个想法。

答案 3 :(得分:0)

在你的where子句中试试这个: staffcode ='Caty'或StaffCode in(选择与Caty同组的工作人员)