我的桌子看起来像这样。
如果roved_by_asm = 1且roved_by_rsm = 0,则mystatus列应为PENDING
否则,如果核准的_by_asm = 0和核准的_by_rsm = 1,则mystatus列应为“待处理”
否则,如果核准的_by_asm = 0和核准的_by_rsm = 0,则mystatus列应为PENDING
否则,如果核准的_by_asm = 1和核准的_by_rsm = 1,则应批准mystatus列
否则mystatus列应为DECLINED。如何使用SQL实现此功能,我将MySQL用作数据库。
这是我的代码:
create view view1
as
select *
case
when approved_by_asm = 0 and approved_by_rsm = 0 THEN 'PENDING'
when approved_by_asm = 0 and approved_by_rsm = 1 THEN 'PENDING'
when approved_by_asm = 1 and approved_by_rsm = 0 THEN 'PENDING'
when approved_by_asm = 1 and approved_by_rsm = 1 THEN 'APPROVED'
else 'DECLINED'
end mystatus
from loan_coooler ;
答案 0 :(得分:0)
请考虑以下内容,如果没有其他说明,则可以通过较少的输入演示相同的逻辑...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(a TINYINT NULL,b TINYINT NULL);
INSERT INTO my_table VALUES
(NULL,NULL),
(0,NULL),
(NULL,0),
(1,NULL),
(NULL,1),
(0,0),
(1,0),
(0,1),
(1,1);
SELECT *
, -- AND NOTE THE COMMA!!!
CASE WHEN a+b < 2 THEN 'pending' WHEN a+b = 2 THEN 'approved' ELSE 'declined' END x
FROM my_table;
+------+------+----------+
| a | b | x |
+------+------+----------+
| NULL | NULL | declined |
| 0 | NULL | declined |
| NULL | 0 | declined |
| 1 | NULL | declined |
| NULL | 1 | declined |
| 0 | 0 | pending |
| 1 | 0 | pending |
| 0 | 1 | pending |
| 1 | 1 | approved |
+------+------+----------+