2016-05-26 84 views
-1

我想要得到什麼將被視爲Excel中嵌套如果功能在查詢中制定。查詢的目標是創建一個視圖,以便我可以比較兩組數據。條件格式如果然後語句

如果在excel中,公式如下所示:= IF(condition1 = 0,condition2,IF(condition2 = 0,condition3,condition1))。

我不斷收到此錯誤信息:

我曾嘗試使用的查詢語言是:

drop view danburycomp 
go 
create view danburycomp as 
SELECT *, 
     TotalCash=CASE 
        WHEN [cash out] = 0    THEN [cash counter cash in] 
        WHEN [cash counter cash in] = 0 THEN [cash counter cash out] 
                ELSE [cash out] 
       END 
FROM [trans jan-mar2016] 
WHERE [account number] IN ('UNIQUEID1', 'UNIQUEID2') 
    AND ([cash into trans] != 0 
     OR [cash out] != 0) 
    AND [date time trans] BETWEEN '2016-01-29' AND '2016-02-24' 
+1

那麼是什麼問題?有什麼錯誤? \t請閱讀[**如何提問**](http://stackoverflow.com/help/how-to-ask) \t \t這裏是[** START **]( http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/)瞭解如何提高您的問題質量並獲得更好的答案。 –

+0

向我們展示db模式和真正的查詢你嘗試...你不能比較DateID與字符串「'DATE''並比較'[COLUMN NAME]!= 0'兩次 –

+0

@JuanCarlosOropeza,感謝您的建議編輯。我已盡力刪除任何個人身份信息。謝謝。 – user2243

回答

0

兩件事情:

  1. 而不是TotalCash=as TotalCashend後在案件陳述中。

  2. 案例邏輯關閉。如果現金出<> 0和[現金櫃臺現金輸入] = 0,它將返回[現金櫃臺現金出],我認爲這不是想要的。

所以用這個:

drop view danburycomp 
go 
create view danburycomp as 
select *, 
case 
    when[Cash Out]=0 and [Cash Counter Cash In]<>0 then [Cash Counter Cash In] 
    when [Cash Out]=0 and[Cash Counter Cash In]=0 then [Cash Counter Cash Out] 
    else [Cash Out] 
end as TotalCash 
from [Trans Jan-Mar2016] 
where [Account Number] in ('UNIQUEID1', 'UNIQUEID2') 
and ([Cash Into Trans] !=0 or [Cash Out]!=0) 
and [Date Time Trans] between '2016-01-29' and '2016-02-24'