2016-09-07 51 views
0

我有一個案例查詢,需要重新編寫它以獲得完美結果。當前查詢爲如下:如何編寫嵌套案例查詢並獲得正確結果

CASE 
WHEN MonthsRetrieved <= 6 AND MonthsRetrieved > 0 THEN 
    CASE 
     WHEN MonthsSinceLastReceipt <= 6 THEN 'Fast' 
     WHEN MonthsSinceLastReceipt > 6 AND MonthsSinceLastReceipt <= 12 THEN 'Slow' 
     WHEN MonthsSinceLastReceipt > 12 AND MonthsSinceLastReceipt <= 18 THEN 'Very Slow' 
     WHEN MonthsSinceLastReceipt > 18 THEN 'Dead' 
     WHEN OpeningQty = 0 THEN 'New Items' 
     WHEN QtyIssued = 0 THEN 'Non-Moving' 
    ELSE 'Fast' 
    END 
WHEN MonthsRetrieved > 6 AND MonthsRetrieved <= 12 THEN 
    CASE 
     WHEN MonthsRetrieved <= 12 THEN 'Slow' 
     WHEN MonthsSinceLastReceipt > 12 AND MonthsSinceLastReceipt <= 18 THEN 'Very Slow' 
     WHEN MonthsSinceLastReceipt > 18 THEN 'Dead' 
     WHEN OpeningQty = 0 THEN 'New Items' 
     WHEN QtyIssued = 0 THEN 'Non-Moving' 
    ELSE 'Slow' 
    END 
WHEN MonthsRetrieved > 12 AND MonthsRetrieved <= 18 THEN 
    CASE 
     WHEN MonthsSinceLastReceipt <= 18 THEN 'Very Slow' 
     WHEN MonthsSinceLastReceipt > 18 THEN 'Dead' 
     WHEN OpeningQty = 0 THEN 'New Items' 
     WHEN QtyIssued = 0 THEN 'Non-Moving' 
    ELSE 'Very Slow' 
    END 
WHEN MonthsRetrieved = 0 THEN 
    CASE 
     WHEN MonthsSinceLastReceipt = 0 THEN 'Dead' 
     WHEN OpeningQty = 0 THEN 'New Items' 
     WHEN QtyIssued = 0 THEN 'Non-Moving' 
    ELSE 'Dead' 
    END 
WHEN MonthsRetrieved > 18 THEN 
    CASE 
     WHEN MonthsSinceLastReceipt > 18 THEN 'Dead' 
     WHEN OpeningQty = 0 THEN 'New Items' 
     WHEN QtyIssued = 0 THEN 'Non-Moving' 
    ELSE 'Dead' 
    END 
END 

我從一個公式得到一個值個月,它是在MonthsRetrieved。 月份的另一個值是MonthsSinceLastReceipt,這是自上次收貨日期以來的月份。 OpeningQty是特定日期的開始數量。 QtyIssued是兩個日期之間發出的數量。

該項目正在分爲快,慢,非常慢,死,新項目,非移動。 case語句是針對每個分類嵌套的。我得到的結果,但他們不正確。我相信我在聲明中忽略了一些東西。

+1

你會得到什麼結果以及它們爲什麼不正確?你能添加樣本數據和預期結果嗎? – HoneyBadger

+1

製作一張限量表。加入。一旦遇到。 – jarlh

+0

我看到的一件事是:'when openqty = 0 then'New Items'' - 當開始數量爲0時,不應該是所有情況下的分類嗎?你寫它的方式,它不會是所有情況下,但只有當其他「WHEN」條件失敗時。 「不移動」也一樣。 – mathguy

回答

1

CASE中的WHEN按順序進行評估。
一旦一個評估爲真,隨後會被忽略。

所以,當你比如做:

CASE WHEN x >= 1 and x < 3 THEN 
    CASE 
    WHEN x = 1 then 'one' 
    WHEN x < 3 then 'two' 
    WHEN y = 0 then 'THIS GETS NEVER EVALUATED' 
    END 
END 

這是第3從來沒有得到評估,因爲以前的檢查之一,必將是真實的。

因此,您可能需要將這些檢查移動到MonthsSinceLastReceipt檢查上方的OpeningQty和QtyIssued上。

並審查如何檢查MonthsSinceLastReceipt。其中一些檢查將在您當前的查詢中始終被忽略。

我建議用這些列名創建一個小樣機表,並用每個案例的值填充。然後使用該表構造一個帶有個案的查詢,直到結果完全如您所期望的那樣。

這是一個嘗試。但是,如果不知道預期的結果應該如何,那只是一種猜測。

delete from TestTable; 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(0,0,0,0); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,0,0,0); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,0,0); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,6,19); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,6,13); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,6,7); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,6,3); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,12,19); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,12,13); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,12,7); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,18,19); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,18,15); 
insert into TestTable (OpeningQty,QtyIssued,MonthsRetrieved,MonthsSinceLastReceipt) values(1,1,19,0); 

select t.*, 
CASE 
WHEN OpeningQty = 0 THEN 'New Items' 
WHEN QtyIssued = 0 THEN 'Non-Moving' 
WHEN MonthsRetrieved = 0 THEN 'Dead' 
WHEN MonthsRetrieved BETWEEN 1 AND 6 THEN 
    (CASE 
    WHEN MonthsSinceLastReceipt > 18 THEN 'Dead' 
    WHEN MonthsSinceLastReceipt > 12 THEN 'Very Slow' 
    WHEN MonthsSinceLastReceipt > 6 THEN 'Slow' 
    ELSE 'Fast' 
    END) 
WHEN MonthsRetrieved BETWEEN 7 AND 12 THEN 
    (CASE 
    WHEN MonthsSinceLastReceipt > 18 THEN 'Dead' 
    WHEN MonthsSinceLastReceipt > 12 THEN 'Very Slow' 
    ELSE 'Slow' 
    END) 
WHEN MonthsRetrieved BETWEEN 13 AND 18 THEN 
    (CASE 
    WHEN MonthsSinceLastReceipt > 18 THEN 'Dead' 
    ELSE 'Very Slow' 
    END) 
WHEN MonthsRetrieved >= 19 THEN 'Dead' 
END as result 
from TestTable t;