根据一列结果显示结果

时间:2017-03-01 11:53:54

标签: sql sql-server-2008

Microsoft SQL Server 2008。

道歉并不确定最好的方式来表达这一点,希望你能得到这个要点!

当他们的基本费率设定为0.00

时,我正在做一件事来提取个人工资率

示例:

Name    | Wage Type  | Amount  
--------+------------+-------
JSmith  | Basic      |  0.00  
JSmith  | Overtime   | 10.00  
JSmith  | Sickness   | 10.00  

我目前的代码是

select 
    w.forenames, w.surname, ty.description, r.amount
from 
    rolesAndRates r 
left join
    workers w on r.owner = w.person 
left join
    rateTypes ty on r.rate = ty.id
where 
    ty.description = 'basic rate' 
    and amount = 0.00

这导致只返回基本费率(原因很明显)。

但是,我希望在一名员工下拥有所有工资类型。

希望这是有道理的!

3 个答案:

答案 0 :(得分:0)

我认为您需要inexists

select w.forenames, w.surname, rt.description, rr.amount
from rolesAndRates rr join
     workers w
     on rr.owner = w.person join
     rateTypes rt
     on rr.rate = rt.id
where exists (select 1
              from rolesAndRates rr2 join
                   rateTypes rt2
                   on rr2.rate = rt2.id
              where rt.description = 'basic rate' and
                    rr.amount = 0.00 and
                    rr2.owner = rr.owner
             );

答案 1 :(得分:0)

很难提出确切的语法,因为你没有提到有问题的表格。但是这样的事情就是你所需要的。

select w.forenames, w.surname, ty.description,r.amount
from rolesAndRates r left join
     workers w
     on r.owner=w.person left join
     rateTypes ty
     on r.rate=ty.id
where 
w.<name column> in ( select <name column> from rateTypes where
description='basic rate' and amount=0.00)

答案 2 :(得分:0)

我认为/希望这就是你想要的。 返回基本费率为零的人的所有费率。

--use CTE to find people with basic of 0.00
with peopleWithZeroBasic as (
    select w.person
    from #rolesAndRates r 
    left join #workers w on r.owner=w.person 
    left join #rateTypes ty on r.rate=ty.id
    where ty.description='basic' and amount=0.00
)

select w.forenames, w.surname, ty.description,r.amount
from #rolesAndRates r 
left join #workers w on r.owner=w.person 
left join #rateTypes ty on r.rate=ty.id
where w.person in (select person from peopleWithZeroBasic)

所以对于数据

create table #rolesAndRates (  rate int, amount decimal, owner varchar(20))
create table #workers ( forenames varchar(max), surname varchar(max), person varchar(20) )
create table #rateTypes (id int, description varchar(max))

insert into #rateTypes (id, description) values (1,'basic')
insert into #rateTypes (id, description) values (2,'overtime')
insert into #rateTypes (id, description) values (3,'sickness')

insert into #workers (forenames, surname, person) values ('john', 'smith', 'jsmith')
insert into #workers (forenames, surname, person) values ('jim', 'beam', 'jbeam')

insert into #rolesAndRates (rate, amount, owner) values (1,0.00,'jsmith')
insert into #rolesAndRates (rate, amount, owner) values (2,10.00,'jsmith')
insert into #rolesAndRates (rate, amount, owner) values (3,5.00,'jsmith')

insert into #rolesAndRates (rate, amount, owner) values (1,7.00,'jbeam')
insert into #rolesAndRates (rate, amount, owner) values (2,10.00,'jbeam')
insert into #rolesAndRates (rate, amount, owner) values (3,5.00,'jbeam');

它产生以下输出:

forenames   surname description amount
john    smith   basic   0
john    smith   overtime    10
john    smith   sickness    5

即。忽略了吉姆光束,因为他没有基本的零速率。