繁体   English   中英

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

[英]If condition is not met substitute for values from a row where the condition is met

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”之外,我需要找到一种方法在任何辅助角色的有效起始日期之前添加最新的主值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM