我有一个商业案例,在表格中我可能有特定的价值,或者它的价值为' ALL'。当我查询表格时,如果特定输入匹配,我需要该特定记录。如果它不匹配则应考虑并返回ALL的条目。
以下是您尝试复制问题的案例。
CREATE TABLE lookup_value (
country VARCHAR2(2) NOT NULL,
department VARCHAR2(30) NOT NULL,
problem_code VARCHAR2(30),
disposition VARCHAR2(30));
INSERT INTO lookup_value VALUES ('US','SALES','P0044','EXCHANGE')
INSERT INTO lookup_value VALUES ('US','SALES','ALL','EXCHANGE')
INSERT INTO lookup_value VALUES ('US','SALES','P0020','ALL')
INSERT INTO lookup_value VALUES ('US','SALES','ALL','ALL')
INSERT INTO lookup_value VALUES ('US','OPERATIONS','P0044','ACKNOELEDGE')
INSERT INTO lookup_value VALUES ('US','OPERATIONS','ALL', 'ALL')
SELECT *
FROM lookup_value
WHERE country = 'US'
AND department = 'SALES'
AND problem_code = DECODE (:problem, problem_code, :problem, 'ALL')
AND disposition = DECODE (:disp, disposition, :disp, 'ALL')
如果我运行上面的查询,传递problem_code = P0044和disposition =' EXCHANGE',它会返回3行,因为我只需要一行。
COUNTRY DEPARTMENT PROBLEM_CODE DISPOSITION
-----------------------------------------------------------
US SALES P0044 EXCHANGE
US SALES ALL EXCHANGE
US SALES ALL ALL
此致 Tauseef