我想檢索以下示例中的數據。以下SQL將返回兩條記錄。SQL子查詢錯誤
SELECT -1 AS NUM
FROM TABLE
WHERE COMP_CODE = 'TEST'
AND (DETL_REMK = 'Rest Day'
OR SHFT_CODE = 'WK_PH')
AND RSRV_DATE_1 IS NOT NULL
AND RSRV_DATE_1 BETWEEN @TR_FR AND @TR_TO
AND EMPE_ID = 'TEST'
GROUP BY EMPE_ID,
RSRV_DATE_1
我想顯示下面。
ORG UNIT EMPE_ID FAM_NAME TMS_TYPE Qty
-----------------------------------------------------------------
'' '' '' Used -1
'' '' '' Used -1
所以,我正在努力做這樣的SQL語句。但是我得到了錯誤 「子查詢返回的值超過1,當子查詢跟隨=,!=,<,< =,>,> =或子查詢用作表達式時,這是不允許的。
如何構建正確的SQL。請幫幫我。提前致謝。
SELECT '' ORG_UNIT, '' EMPE_ID, '' FAM_NAME, 'Used' AS TMS_TYPE,
ISNULL(CAST(
(SELECT -1 AS NUM
FROM TABLE
WHERE COMP_CODE = 'TEST'
AND (DETL_REMK = 'Rest Day'
OR SHFT_CODE = 'WK_PH')
AND RSRV_DATE_1 IS NOT NULL
AND RSRV_DATE_1 BETWEEN @TR_FR AND @TR_TO
AND EMPE_ID = 'TEST'
GROUP BY EMPE_ID, RSRV_DATE_1)AS NVARCHAR(MAX)),0) QTY
明白了。非常感謝。 –