【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
第二步:創建測試數據進行測試
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表數據
映射表數據
查詢結構
該案例適用於存在映射表,然後根據映射表匹配得出結果!
【DB2】根據映射表映射出結果