2015-11-12 47 views
0

我有一個查詢,我已經做了一個case語句,但我需要一個基於case語句結果的參數。在DB2上設置case語句的參數?

我正在使用DB2,任何人都可以建議嗎?

SELECT DRIVER_ID, NAME, DRIVER.USER9 AS PHONE, 
CASE WHEN TERMINAL_NUMBER IN ('42','9','75','54','52','40','42','41','6') THEN 'EAST' 
WHEN TERMINAL_NUMBER IN ('0','8','43','48','47','2','46','50','4','5','49', '1') THEN 'WEST' 
ELSE '' END AS REGION 
, DRIVER_TYPE 
FROM DRIVER WHERE ACTIVE_IN_DISP = 'True' AND 
<PARAMETER WOULD GO HERE> 
ORDER BY TERMINAL_NUMBER 
WITH UR 

正如你可以看到我有一個情況下,東方或西方兩種結果,我想這些是參數。

預先感謝您!

回答

1

您可以創建一個公用表表達式來使用case語句的結果作爲參數。

主要使用原始選擇語句。

with MyCTE as (
SELECT DRIVER_ID, NAME, DRIVER.USER9 AS PHONE, 
    CASE WHEN TERMINAL_NUMBER IN ('42','9','75','54','52','40','42','41','6') THEN 'EAST' 
    WHEN TERMINAL_NUMBER IN ('0','8','43','48','47','2','46','50','4','5','49', '1') THEN 'WEST' 
    ELSE '' END AS REGION 
    , DRIVER_TYPE 
    FROM DRIVER WHERE ACTIVE_IN_DISP = 'True' 
    ) 
    select * from MyCTE where region = ?