我想/希望這是你想要的。 返回基礎費率爲零的人的所有費用。
--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