聚合函数或 GROUP BY 子句

Posted

技术标签:

【中文标题】聚合函数或 GROUP BY 子句【英文标题】:An aggregate function or the GROUP BY clause 【发布时间】:2020-01-20 06:34:33 【问题描述】:

我使用了以下查询:

SELECT DISTINCT 
    b.strCostCentreID, b.strPOSOnlineRefNo, b.strPayTypeCode,
    tblMachine.strDesc AS KioskName,
    (SUM(b.dblPaidAmt)) AS RM,
    (SUM(b.dblIpay88Amt)) AS pay88AmtRM,
    (SELECT STUFF((select   ','  + strProdType
     FROM tblCurrTrx AS a
     WHERE a.strPOSOnlineRefNo = b.strPOSOnlineRefNo FOR XML PATH('')), 1, 1,'')) AS Agency
FROM 
    tblCurrTrx AS b
INNER JOIN 
    tblMachine ON b.strMachID = tblMachine.strMachID
WHERE 
    b.strPaymentMethod = '2' 
    AND (dtmTrans >= '1/3/2020') 
    AND (strTransStatus = '01')
GROUP BY 
    b.strPOSOnlineRefNo

但我收到以下错误:

消息 8120,第 16 级,状态 1,第 1 行 列 'tblCurrTrx.strCostCentreID' 在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

【问题讨论】:

GROUP BY b.strCostCentreID , b.strPOSOnlineRefNo, b.strPayTypeCode, tblMachine.strDesc. 非常感谢 Akina 第一次工作。现在来了解这一点! 【参考方案1】:

请按字段添加分组

    select distinct b.strCostCentreID ,  b.strPOSOnlineRefNo, b.strPayTypeCode, tblMachine.strDesc as KioskName, 
         (SUM(b.dblPaidAmt)) as RM ,(SUM(b.dblIpay88Amt)) As pay88AmtRM ,

        (Select STUFF((select   ','  +strProdType from tblCurrTrx as a  where a.strPOSOnlineRefNo = b.strPOSOnlineRefNo for xml path('')),1,1,'')) as Agency


        From tblCurrTrx as b INNER JOIN tblMachine ON b.strMachID = tblMachine.strMachID WHERE  

        b.strPaymentMethod = '2' and (dtmTrans>='1/3/2020') AND (strTransStatus='01') 

group by b.strCostCentreID,b.strPOSOnlineRefNo, b.strPayTypeCode, tblMachine.strDesc

【讨论】:

【参考方案2】:

试试这个:

SELECT DISTINCT b.strCostCentreID
               ,b.strPOSOnlineRefNo
               ,b.strPayTypeCode
               ,tblMachine.strDesc AS KioskName
               ,SUM(b.dblPaidAmt) AS RM
               ,SUM(b.dblIpay88Amt) AS pay88AmtRM
              ,DS.Agency
FROM tblCurrTrx AS b
INNER JOIN tblMachine 
    ON b.strMachID = tblMachine.strMachID
OUTER APPLY
(
    SELECT STUFF
    (
        (
            select   ','  + strProdType
            FROM tblCurrTrx AS a
            WHERE a.strPOSOnlineRefNo = b.strPOSOnlineRefNo 
            FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)')
        ,1
        ,1
        ,''
    )
) DS(Agency)
WHERE b.strPaymentMethod = '2' 
    AND dtmTrans >= '1/3/2020' 
    AND strTransStatus = '01'
GROUP BY b.strCostCentreID
        ,b.strPOSOnlineRefNo
        ,b.strPayTypeCode
        ,tblMachine.strDesc
        ,DS.Agency

【讨论】:

【参考方案3】:

如果您使用 group by 子句而不是 select 语句列应包含在 group by 中,即

select distinct b.strCostCentreID ,  b.strPOSOnlineRefNo, b.strPayTypeCode, tblMachine.strDesc as KioskName, 
 (SUM(b.dblPaidAmt)) as RM ,(SUM(b.dblIpay88Amt)) As pay88AmtRM ,

(Select STUFF((select   ','  +strProdType from tblCurrTrx as a  where a.strPOSOnlineRefNo = b.strPOSOnlineRefNo for xml path('')),1,1,'')) as Agency


From tblCurrTrx as b INNER JOIN tblMachine ON b.strMachID = tblMachine.strMachID WHERE  

b.strPaymentMethod = '2' and (dtmTrans>='1/3/2020') AND (strTransStatus='01') group by b.strPOSOnlineRefNo, b.strCostCentreID , b.strPayTypeCode, tblMachine.strDesc 

【讨论】:

在 GROUP BY 中两次提及 b.strPOSOnlineRefNo 是多余的。

以上是关于聚合函数或 GROUP BY 子句的主要内容,如果未能解决你的问题,请参考以下文章

15group by子句与聚合函数

选择列表中的无效表达式(不包含在聚合函数或 GROUP BY 子句中)

查询没有重复和聚合函数或 GROUP BY 子句问题。 - 重复

列“在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中”

SQL Server报错:选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中

列的原因在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中[重复]