2017-03-01 202 views
1

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 

這導致返回基本費率(出於顯而易見的原因)。

但是我希望擁有一名員工的所有工資類型。

希望這是有道理的!

+0

您的預期成果是什麼? – Wanderer

+0

向我們展示給出結果的表格數據。 – jarlh

+0

預期的結果是帖子的表格,它所有來自的基本上是相同的,但一些僱員的一些誰將有一個基本利率> 0.00 – Jim

回答

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 

即忽略吉姆束,因爲他不具有零的基本速率。

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 
      ); 
0

這是很難拿出確切的語法,你有沒有在問題中提到人lthe表。但是這樣的事情就是你需要的。

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) 
+0

謝謝大家到目前爲止..我可能不那麼清楚在我的詳細信息,我希望在基本利率爲0.00時顯示所有工資類型 – Jim