【DB2】根据映射表映射出结果
第一步:创建语法
CREATE TABLE OLIVER_MAP(ID INT,COM_TYPE VARCHAR(100),COM_NAME VARCHAR(100),SR_UP DECIMAL(18,2), SR_LIM DECIMAL(18,2),RES VARCHAR(100)); INSERT INTO OLIVER_MAP(ID,COM_TYPE,COM_NAME,SR_UP,SR_LIM,RES) VALUES(1,'A1','A类小型公司',3000000,NULL,'AS'); INSERT INTO OLIVER_MAP(ID,COM_TYPE,COM_NAME,SR_UP,SR_LIM,RES) VALUES(2,'A2','A类小型公司',3000000,NULL,'AS'); INSERT INTO OLIVER_MAP(ID,COM_TYPE,COM_NAME,SR_UP,SR_LIM,RES) VALUES(3,'B1','B类大型公司',100000000,NULL,'BB'); INSERT INTO OLIVER_MAP(ID,COM_TYPE,COM_NAME,SR_UP,SR_LIM,RES) VALUES(4,'B2','B类大型公司',100000000,NULL,'BB'); INSERT INTO OLIVER_MAP(ID,COM_TYPE,COM_NAME,SR_UP,SR_LIM,RES) VALUES(5,NULL,null,null,NULL,'O');
CREATE TABLE OLIVER_01(ID INT,COM_TYPE VARCHAR(100),SR DECIMAL(18,2),YQFL VARCHAR(10)); INSERT INTO OLIVER_01 VALUES(1,'A1',30000000,'O'); INSERT INTO OLIVER_01 VALUES(2,'A2',30000000,'O'); INSERT INTO OLIVER_01 VALUES(3,'B1',100000000,'BB'); INSERT INTO OLIVER_01 VALUES(4,'B2',100000000,'BB'); INSERT INTO OLIVER_01 VALUES(5,'C',100000000,'O'); INSERT INTO OLIVER_01 VALUES(6,'A1',NULL,'O'); INSERT INTO OLIVER_01 VALUES(7,'A2',NULL,'O'); INSERT INTO OLIVER_01 VALUES(8,'B1',NULL,'O'); INSERT INTO OLIVER_01 VALUES(9,'B2',NULL,'O'); INSERT INTO OLIVER_01 VALUES(10,'A1',30000001,'O');
第三步骤:查询结果
SELECT T.* ,(SELECT RES FROM OLIVER_MAP A WHERE (A.COM_TYPE IS NULL OR A.COM_TYPE=T.COM_TYPE) AND (A.SR_UP IS NULL OR A.SR_UP>=T.SR) AND (A.SR_LIM IS NULL OR A.SR_LIM<T.SR) ORDER BY A.ID FETCH FIRST ROW ONLY) AS RES FROM OLIVER_01 T
OLIVER_01表数据
映射表数据
查询结构
该案例适用于存在映射表,然后根据映射表匹配得出结果!
作者:奔跑的金鱼
声明:书写博客不易,转载请注明出处,请支持原创,侵权将追究法律责任
个性签名:人的一切的痛苦,本质上都是对自己无能的愤怒
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!