如果不满足条件,则替换满足条件的行中的值

时间:2016-07-25 09:57:19

标签: mysql conditional case

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日增加了第二个临时职位 我需要一个返回任何角色的查询以及最近的主要角色及其合约时间:

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

但是,除了“else Null”之外,我需要找到一种方法,在任何次要角色的有效起始日期之前添加最新的主要值。

1 个答案:

答案 0 :(得分: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 |
+----------+--------+---------------+---------------+------------+------------+----------+--------+---------------+---------------+------------+