2013-10-14 100 views
-1

我想創建一個從RECIPT表中獲取RECID的查詢,它是Customer表中的custname和RECIPT上的RECID的最大值。但不知何故,這個查詢會引發錯誤。當我添加max(MAX(REC.REC_Id_I)作爲RECID)時開始。如何在Join查詢中使用MAX()

查詢:

SELECT 
    REC.REC_Id_I, 
    REC.REC_No_V, 
    CUS.CUS_Name_V, 
    REC.REC_PaidAmount_M, 
    CASE 
     WHEN REC.REC_PayMode_C = 'C' THEN 'Cash' 
     ELSE 'Cheque' 
    END AS REC_PayMode_C, 
    REC.REC_BankName_V, 
    REC.REC_BankAddress, 
    REC.REC_ChequeNo_V, 
    convert(varchar, REC.REC_ChequeDate_D, 103) AS REC_ChequeDate_D, 
    REC.REC_Date_D, 
    MAX(REC.REC_Id_I) As RECID 
FROM TBL_Receipts REC 
JOIN TBL_CustomerMaster CUS ON REC.REC_CustomerId_I = CUS.CUS_Id_I 
WHERE 
    REC_Active_c='y' 
    AND REC_SalesMasterId_I='0' 
ORDER BY REC_Id_I 

錯誤是:

消息8120,級別16,狀態1,過程sp_Receipt_Select,行15列 'TBL_Receipts.REC_Id_I' 在選擇列表中,因爲無效它不包含在聚合函數或GROUP BY子句中。

+1

MSSQL不是MySQL。你需要明確地「GROUP BY」你的列。 –

+0

@Juhana錯誤消息表明它是MSSQL。 MySQL會隱式地將GROUP BY作爲列。 –

+0

與其他地方使用「GROUP BY」的方法相同。 –

回答

2

如果你仍然有你的GROUP BY子句中的問題,你可以嘗試這樣的事情,但它不是一個最佳的解決方案

SELECT 
    REC.REC_Id_I, 
    REC.REC_No_V, 
    CUS.CUS_Name_V, 
    REC.REC_PaidAmount_M, 
    CASE 
     WHEN REC.REC_PayMode_C = 'C' THEN 'Cash' 
     ELSE 'Cheque' 
    END AS REC_PayMode_C, 
    REC.REC_BankName_V, 
    REC.REC_BankAddress, 
    REC.REC_ChequeNo_V, 
    convert(varchar, REC.REC_ChequeDate_D, 103) AS REC_ChequeDate_D, 
    REC.REC_Date_D, 
    recid.maxRECID As RECID 
FROM TBL_Receipts REC 
JOIN TBL_CustomerMaster CUS ON REC.REC_CustomerId_I = CUS.CUS_Id_I 
JOIN (SELECT MAX(REC_ID_I) as maxRECID, REC_CustomerId_I FROM REC group by REC_CustomerId_I) as recid ON recid.REC_CustomerId_I = REC.REC_CustomerId_I 
WHERE 
    REC_Active_c='y' 
    AND REC_SalesMasterId_I='0' 
ORDER BY REC_Id_I 
1

你幾乎需要按聚合函數以外的所有選定列進行分組。如果你對結果的顯示方式,應該是有意義的,你所有的列應是該組由:

SELECT 
    REC.REC_Id_I, 
    REC.REC_No_V, 
    CUS.CUS_Name_V, 
    REC.REC_PaidAmount_M, 
    CASE 
     WHEN REC.REC_PayMode_C = 'C' THEN 'Cash' 
     ELSE 'Cheque' 
    END AS REC_PayMode_C, 
    REC.REC_BankName_V, 
    REC.REC_BankAddress, 
    REC.REC_ChequeNo_V, 
    convert(varchar, REC.REC_ChequeDate_D, 103) AS REC_ChequeDate_D, 
    REC.REC_Date_D, 
    MAX(REC.REC_Id_I) As RECID 
FROM TBL_Receipts REC 
JOIN TBL_CustomerMaster CUS ON REC.REC_CustomerId_I = CUS.CUS_Id_I 
WHERE 
    REC_Active_c='y' 
    AND REC_SalesMasterId_I='0' 
GROUP BY 
    REC.REC_Id_I, 
    REC.REC_No_V, 
    CUS.CUS_Name_V, 
    REC.REC_PaidAmount_M, 
    CASE 
     WHEN REC.REC_PayMode_C = 'C' THEN 'Cash' 
     ELSE 'Cheque' 
    END AS REC_PayMode_C, 
    REC.REC_BankName_V, 
    REC.REC_BankAddress, 
    REC.REC_ChequeNo_V, 
    convert(varchar, REC.REC_ChequeDate_D, 103) AS REC_ChequeDate_D, 
    REC.REC_Date_D 
ORDER BY REC_Id_I 
0

什麼錯誤,你讓當您添加GROUP BY條款?永遠不要寫出你得到錯誤,始終寫錯誤是什麼。

我不知道你是如何與GROUP BY條款寫您的查詢(因爲你沒有它向我們展示),但試試這個:

SELECT 
    REC.REC_Id_I, 
    REC.REC_No_V, 
    CUS.CUS_Name_V, 
    REC.REC_PaidAmount_M, 
    CASE 
     WHEN REC.REC_PayMode_C = 'C' THEN 'Cash' 
     ELSE 'Cheque' 
    END AS REC_PayMode_C, 
    REC.REC_BankName_V, 
    REC.REC_BankAddress, 
    REC.REC_ChequeNo_V, 
    convert(varchar, REC.REC_ChequeDate_D, 103) AS REC_ChequeDate_D, 
    REC.REC_Date_D, 
    MAX(REC.REC_Id_I) As RECID 
FROM TBL_Receipts REC 
JOIN TBL_CustomerMaster CUS ON REC.REC_CustomerId_I = CUS.CUS_Id_I 
WHERE 
    REC_Active_c='y' 
    AND REC_SalesMasterId_I='0' 
GROUP BY 
    REC.REC_Id_I, 
    REC.REC_No_V, 
    CUS.CUS_Name_V, 
    REC.REC_PaidAmount_M, 
    CASE 
     WHEN REC.REC_PayMode_C = 'C' THEN 'Cash' 
     ELSE 'Cheque' 
    END, 
    REC.REC_BankName_V, 
    REC.REC_BankAddress, 
    REC.REC_ChequeNo_V, 
    convert(varchar, REC.REC_ChequeDate_D, 103), 
    REC.REC_Date_D 
ORDER BY REC_Id_I; 
0

它應該甚至工作通過使用select語句我解決我的問題

Code is:

SELECT 
    REC.REC_Id_I, 
    REC.REC_No_V, 
    CUS.CUS_Name_V, 
    REC.REC_PaidAmount_M, 
    CASE WHEN REC.REC_PayMode_C='C' THEN 'Cash' ELSE 'Cheque' END AS REC_PayMode_C, 
    REC.REC_BankName_V, 
    REC.REC_BankAddress, 
    REC.REC_ChequeNo_V, 
    convert(varchar, REC.REC_ChequeDate_D, 103) AS REC_ChequeDate_D, 
    REC.REC_Date_D,`(SELECT MAX(REC_Id_I) from TBL_Receipts) AS RECID` 
    FROM TBL_Receipts REC 
    JOIN TBL_CustomerMaster CUS 
    on REC.REC_CustomerId_I=CUS.CUS_Id_I where REC_Active_c='y' and REC_SalesMasterId_I='0' 
    ORDER BY REC_Id_I 
0

要計算聚合函數,並讓你(在其上計算總的項目除外)必須具有所有其他列在GROUP BY相應的列值。

SELECT table_1.column_A, 
     MAX(table_2.column_B), 
     table_2.column_C 

FROM table_1 
    INNER JOIN table_2 ON table_1.column_A=table_2.column_B 

GROUP BY table_1.column_A, 
     table_2.column_C