【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表数据

image

映射表数据

image

查询结构

image

该案例适用于存在映射表,然后根据映射表匹配得出结果!

posted @ 2017-08-10 18:10  OLIVER_QIN  阅读(693)  评论(0编辑  收藏  举报