2014-10-08 46 views
0

我正在寫一個SP服務器的開始,使用幾個case語句和臨時表,並在最後使用一個條件case else語句來基於條件案例else語句。Microsoft SQL server CASE ELSE CASE不正確關閉

這一切都工作正常,直到我到了條件案件else語句由於某種原因結束不正確。

正如您從條件案例else語句中看到的那樣,在END之後的括號中只有一個語法錯誤。

任何意見將不勝感激。

在此先感謝。

--Creates a temp table to store the results of the cases below ans inserts them into the table. 
CREATE TABLE #TotalBySlSsCs(

    BoughtTotal FLOAT NULL, 
    SoldTotal FLOAT NULL, 
    SSaleTotal FLOAT NULL, 
    CShortTotal FLOAT NULL, 
    TotalQuanity Float NULL 
) 

INSERT INTO #TotalBySlSsCs(BoughtTotal, SoldTotal, SSaleTotal, CShortTotal) 

-- Cases to total the quanities for portfilios and securities based on the transcode is in ('by', 'sl', 'ss', 'cs') plus the portfolio, security, and date selected by the user. 
SELECT 
    SUM(CASE WHEN FundTransactions.TransCode='by' THEN (CAST(FundTransactions.Quantity AS FLOAT)) ELSE 0 END) AS 'BoughtTotal', 
    SUM(CASE WHEN FundTransactions.TransCode='sl' THEN (CAST(FundTransactions.Quantity AS FLOAT)) ELSE 0 END) AS 'SoldTotal', 
    SUM(CASE WHEN FundTransactions.TransCode='ss' THEN (CAST(FundTransactions.Quantity AS FLOAT)) ELSE 0 END) AS 'SSaleTotal', 
    SUM(CASE WHEN FundTransactions.TransCode='cs' THEN (CAST(FundTransactions.Quantity AS FLOAT)) ELSE 0 END) AS 'CShortTotal' 

FROM FundTransactions 
--The Where section will contain @parameters to let users select the portfolio, security, and date they need. 
WHERE FundTransactions.PortfolioCode = 'EFS' AND FundTransactions.SecSymbol = 'VXX' AND FundTransactions.TradeDate= '2014-06-30' 
GROUP BY FundTransactions.TransCode 

--Group on Total Quanity to remove the second uneeded row which is created by the above cases and inserts it into temp table #TotalQuanities for calculations.. 
CREATE TABLE #TotalQuanities(
    TotalQuanity VARCHAR(4) NULL, 
    TotalBy FLOAT NULL, 
    TotalSl FLOAT NULL, 
    TotalSs FLOAT NULL, 
    TotalCs FLOAT NULL 
) 

INSERT INTO #TotalQuanities(TotalQuanity, TotalBy, TotalSl, TotalSs, TotalCs) 

SELECT #TotalBySlSsCs.TotalQuanity, MAX(#TotalBySlSsCs.BoughtTotal), MAX(#TotalBySlSsCs.SoldTotal), MAX(#TotalBySlSsCs.SSaleTotal), MAX(#TotalBySlSsCs.CShortTotal) 

FROM #TotalBySlSsCs 
GROUP BY #TotalBySlSsCs.TotalQuanity 

--Case statement for subtracting the final figures from each other based on conditions within the case statement. 
SELECT #TotalQuanities.TotalQuanity, 
    SUM(CASE WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl = 0 AND #TotalQuanities.TotalSs = 0 AND #TotalQuanities.TotalCs = 0 
     THEN 0 

     ELSE 

     CASE WHEN #TotalQuanities.TotalBy != 0 AND #TotalQuanities.TotalSl != 0 AND #TotalQuanities.TotalSs != 0 AND #TotalQuanities.TotalCs != 0 
     THEN #TotalQuanities.TotalBy - #TotalQuanities.TotalSl - #TotalQuanities.TotalSs - #TotalQuanities.TotalCs 

     ELSE 

     CASE WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl != 0 AND #TotalQuanities.TotalSs != 0 AND #TotalQuanities.TotalCs != 0 
     THEN #TotalQuanities.TotalSl - #TotalQuanities.TotalSs - #TotalQuanities.TotalCs 

     ELSE 

     CASE WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl = 0 AND #TotalQuanities.TotalSs != 0 AND #TotalQuanities.TotalCs != 0 
     THEN #TotalQuanities.TotalSs - #TotalQuanities.TotalCs 

     ELSE 

     CASE WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl = 0 AND #TotalQuanities.TotalSs = 0 AND #TotalQuanities.TotalCs != 0 
     THEN #TotalQuanities.TotalCs 

     END) AS 'TOTAL' 


FROM #TotalQuanities 
GROUP BY #TotalQuanities.TotalQuanity 

--Drops the temp tables. 
DROP TABLE #TotalBySlSsCs 
DROP TABLE #TotalQuanities 

回答

0

在多個條件的情況下,您不會重複case。試試這個:

SUM(CASE WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl = 0 AND #TotalQuanities.TotalSs = 0 AND #TotalQuanities.TotalCs = 0 
    THEN 0 

    WHEN #TotalQuanities.TotalBy != 0 AND #TotalQuanities.TotalSl != 0 AND #TotalQuanities.TotalSs != 0 AND #TotalQuanities.TotalCs != 0 
    THEN #TotalQuanities.TotalBy - #TotalQuanities.TotalSl - #TotalQuanities.TotalSs - #TotalQuanities.TotalCs 

    WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl != 0 AND #TotalQuanities.TotalSs != 0 AND #TotalQuanities.TotalCs != 0 
    THEN #TotalQuanities.TotalSl - #TotalQuanities.TotalSs - #TotalQuanities.TotalCs 

    WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl = 0 AND #TotalQuanities.TotalSs != 0 AND #TotalQuanities.TotalCs != 0 
    THEN #TotalQuanities.TotalSs - #TotalQuanities.TotalCs 

    WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl = 0 AND #TotalQuanities.TotalSs = 0 AND #TotalQuanities.TotalCs != 0 
    THEN #TotalQuanities.TotalCs 

    END) AS TOTAL 

此外,你不應該使用單引號列或表別名,但那是另一回事。

+0

感謝您的幫助戈登替換的情況下,欣賞它。單引號也是非常好的一點。我的懶惰。 – 2014-10-08 11:49:15

1

下面

SELECT #TotalQuanities.TotalQuanity, 
    SUM(CASE WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl = 0 AND #TotalQuanities.TotalSs = 0 AND #TotalQuanities.TotalCs = 0 
     THEN 0 
     WHEN #TotalQuanities.TotalBy != 0 AND #TotalQuanities.TotalSl != 0 AND #TotalQuanities.TotalSs != 0 AND #TotalQuanities.TotalCs != 0 
     THEN #TotalQuanities.TotalBy - #TotalQuanities.TotalSl - #TotalQuanities.TotalSs - #TotalQuanities.TotalCs 
     WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl != 0 AND #TotalQuanities.TotalSs != 0 AND #TotalQuanities.TotalCs != 0 
     THEN #TotalQuanities.TotalSl - #TotalQuanities.TotalSs - #TotalQuanities.TotalCs 
     WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl = 0 AND #TotalQuanities.TotalSs != 0 AND #TotalQuanities.TotalCs != 0 
     THEN #TotalQuanities.TotalSs - #TotalQuanities.TotalCs 
     WHEN #TotalQuanities.TotalBy = 0 AND #TotalQuanities.TotalSl = 0 AND #TotalQuanities.TotalSs = 0 AND #TotalQuanities.TotalCs != 0 
     THEN #TotalQuanities.TotalCs 

     END) AS 'TOTAL' 
FROM #TotalQuanities 
GROUP BY #TotalQuanities.TotalQuanity