2016-07-25 13 views
0

HALLO In meinem SQL erfüllt Ich habe das Problem folgenden aus einer Quelltabelle ähnlich die folgenden:Wenn diese Bedingung nicht Ersatz für Werte aus einer Reihe erfüllt, wenn die Bedingung

Tabelle 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 

Also im Beispiel von Mitarbeiter 1 begann er am 1. März 2015 bei einem Unternehmen zu arbeiten, änderte seine Position am 1. Juni 2016 und hatte am 1. Juli 2017 eine zweite beiläufige Rolle Ich brauche eine Abfrage, die alle Rollen zurückgibt und die neueste Hauptrolle mit seinen Vertragszeiten:

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 

Das beste Ergebnis, das ich zurückkehrt einen Nullwert für die zusätzliche Spalte, wenn die primäre Rolle 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” 

Von Tabelle 1

Doch statt die „else Null“ Ich brauche ein finden Möglichkeit, den letzten Primärwert vor dem Gültig-ab-Datum einer sekundären Rolle hinzuzufügen.

Antwort

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' 

Ergebnis

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