2016-07-25 17 views
0

HIを満たしている行の値の代わりに満たされていない場合、私は次のようなソース表から、以下のような問題 があります条件は条件が私のSQLで

表1:

Employee Role Contract Hours IS primary Role Valid from 
1 Role A 35 Y 01/03/2015 
1 Role B 35 Y 01/06/2016 
1 Role C 0 N 01/07/2016 
2 Role A 20 Y 01/01/2016 
2 Role B 0 N 01/01/2016 
2 Role C 25 Y 01/04/2016 
3 Role A 35 Y 01/04/2016 

従業員1の例では、2015年3月1日に会社に勤務し、2016年6月1日に職位を変更し、2017年7月1日に2番目のカジュアルな役割を追加しました。 任意の役割を返すクエリと、契約時間の主な役割:

Employee Role Contract Hours Valid from Primary Role – Contract Hours 
1 Role A 35 01/03/2015 Role A – 35 
1 Role B 35 01/06/2016 Role B – 35 
1 Role C 0 01/07/2016 Role B – 35 
2 Role A 20 01/01/2016 Role A – 20 
2 Role B 0 01/01/2016 Role A – 20 
2 Role C 25 01/04/2016 Role C – 25 
3 Role A 35 01/04/2016 Role A – 35 

私が主な役割は、N

Select 
“Employee”, 
“Role”, 
“Contract Hours”, 
“Valid From”, 
Case 
When “Is primary Role” = Y 
Then “Role” + ‘-‘ + “Contract Hours” 
    Else NULL 
END as “Primary Role – Contract Hours” 

であれば表1

から返します追加の列にNULL値を持っていますが、代わりに私が見つける必要がある「他ヌル」の最良の結果は、セカンダリロールの有効開始日の前に最新のプライマリ値を追加する方法。

答えて

0
/* 
DROP TABLE T; 
CREATE TABLE T (Employee int, Role varchar(10), ContractHours int, ISprimaryRole char(1), Validfrom date); 
insert into t values 
(1, 'Role A', 35 , 'Y' , '2015-03-01'), 
(1, 'Role B', 35 , 'Y' , '2016-06-01'), 
(1, 'Role C', 0 , 'N' , '2016-07-01'), 
(2, 'Role A', 20 , 'Y' , '2016-01-01'), 
(2, 'Role B', 0 , 'N' , '2016-01-01'), 
(2, 'Role C' , 25 , 'Y' , '2016-04-01'), 
(3, 'Role A' , 35 , 'Y' , '2016-04-01'); 
*/ 


select * 
from 
(
select *, 
     (Select max(t1.validfrom) from t t1 where t1.Employee = t.employee and t1.isprimaryrole = 'Y' and t1.Validfrom <= t.validfrom) maxisp 
from t 
) s 
left outer join t t1 on t1.employee = s.employee and t1.Validfrom = s.maxisp and t1.isprimaryrole = 'Y' 

結果

+----------+--------+---------------+---------------+------------+------------+----------+--------+---------------+---------------+------------+ 
| Employee | Role | ContractHours | ISprimaryRole | Validfrom | maxisp  | Employee | Role | ContractHours | ISprimaryRole | Validfrom | 
+----------+--------+---------------+---------------+------------+------------+----------+--------+---------------+---------------+------------+ 
|  1 | Role A |   35 | Y    | 2015-03-01 | 2015-03-01 |  1 | Role A |   35 | Y    | 2015-03-01 | 
|  1 | Role B |   35 | Y    | 2016-06-01 | 2016-06-01 |  1 | Role B |   35 | Y    | 2016-06-01 | 
|  1 | Role C |    0 | N    | 2016-07-01 | 2016-06-01 |  1 | Role B |   35 | Y    | 2016-06-01 | 
|  2 | Role A |   20 | Y    | 2016-01-01 | 2016-01-01 |  2 | Role A |   20 | Y    | 2016-01-01 | 
|  2 | Role B |    0 | N    | 2016-01-01 | 2016-01-01 |  2 | Role A |   20 | Y    | 2016-01-01 | 
|  2 | Role C |   25 | Y    | 2016-04-01 | 2016-04-01 |  2 | Role C |   25 | Y    | 2016-04-01 | 
|  3 | Role A |   35 | Y    | 2016-04-01 | 2016-04-01 |  3 | Role A |   35 | Y    | 2016-04-01 | 
+----------+--------+---------------+---------------+------------+------------+----------+--------+---------------+---------------+------------+