我想/希望这是你想要的。 返回基础费率为零的人的所有费用。
--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
即忽略吉姆束,因为他不具有零的基本速率。
您的预期成果是什么? – Wanderer
向我们展示给出结果的表格数据。 – jarlh
预期的结果是帖子的表格,它所有来自的基本上是相同的,但一些雇员的一些谁将有一个基本利率> 0.00 – Jim