これはあなたが望むものだと思います。 基本料金がゼロの人のすべての料金を返します。彼はゼロの基本的な速度を有していないよう
forenames surname description amount
john smith basic 0
john smith overtime 10
john smith sickness 5
すなわちジム・ビームを無視:データ
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');
それは以下の出力を生成するためのよう
--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)
。
期待どおりの結果が得られましたか? – Wanderer
その結果を示す表データを表示します。 – jarlh
期待される結果は投稿のものですが、それはすべて本質的に同じですが、基本的な割合が0.00よりも高い従業員の数です – Jim