-1

我想创建一个从 RECIPT 表中获取 RECID 的查询,它是来自 Customer 表的 custname 和 RECIPT 上的最大 RECID。但不知何故,这个查询会引发错误。它从我添加 max(MAX(REC.REC_Id_I) As RECID) 开始。

查询是:

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 子句中。

4

5 回答 5

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
于 2013-10-14T10:29:12.320 回答
1

您几乎需要按除聚合函数之外的所有选定列进行分组。如果您考虑如何显示结果,那么所有列都应该在 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 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
于 2013-10-14T07:11:54.170 回答
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 
于 2013-10-14T10:41:06.023 回答
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;
于 2013-10-14T07:26:41.033 回答
0

To calculate aggregate function and to get the corresponding column values you must have all the other columns in GROUP BY (except the ones on which you are calculating the aggregate).

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
于 2013-10-14T11:34:24.443 回答