我需要運行以下兩個查詢上的Sybase引擎內執行兩項(具有不同的條件):相同的SQL請求
SELECT COUNT(*) AS BTB_YES FROM FXMM_EVT_DBF WHERE M__INTID_ = '1.511' AND M_ISHEDGE = 1
SELECT COUNT(*) AS BTB_NO FROM FXMM_EVT_DBF WHERE M__INTID_ = '1.511' AND M_ISHEDGE = 0
因爲我基本上選擇一組相同的信息,即:
SELECT M_ISHEDGE
FROM FXMM_EVT_DBF
WHERE M__INTID_ = '1.511'
然後我用兩個單獨的條件計算這個列兩次,我覺得有一種方法可以在同一個單一查詢上得到結果,這將是很好的,因爲上述查詢必須在多個數據庫上運行。
但是,我無法獲得共同的結果,可能是因爲我不擅長SQL。我的嘗試是:
SELECT M_ISHEDGE
FROM FXMM_EVT_DBF WHERE M__INTID_ = '1.511'
CASE WHEN M_ISHEDGE = 1
then count(*)
else 0
END AS BTB_YES
CASE WHEN M_ISHEDGE = 0
then count(*)
else 0
END AS BTB_NO
但上述失敗,出現以下錯誤:
An error occurred when executing the SQL command:
SELECT M_ISHEDGE
FROM FXMM_EVT_DBF WHERE M__INTID_ = '1.511'
CASE WHEN M_ISHEDGE = 1
then count(*)
else 0
END AS BTB_Y...
Incorrect syntax near the keyword 'CASE'. [SQL State=ZZZZZ, DB Errorcode=156]
Execution time: 0s
1 statement(s) failed.
和SQL編輯器似乎並沒有認識到關鍵字THEN
,雖然它是文檔的一部分,所以可能由於語法錯誤。
由於您想在M_ISHEDGE = 1或0時進行計數,只需按該列進行分組。計數錯誤計算正確,你生病得到兩行結果(每M_ISHEDGE值一個) – jean