0

我正在运行以下 SQL,但不断收到错误消息:-

'列 'SRVS.dbo.BTQGLDistributionsWithProjectCodesHM.AccountNo' 在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。

我正在运行的代码是:-

SELECT         AccountNo, 
           TransDate, 
           JournalNo, 
           AuditTrail, 
           Description, 
           JnlAmount, 
           ACTINDX, 
           CONTRACTNAME, 
           PAPROJNUMBER,GROUPING(PAPROJNUMBER) as PAPROJNUMBER, 
           SUM(PAFeebillamount) as total
FROM       SRVS.dbo.BTQGLDistributionsWithProjectCodesHM
GROUP BY   PAPROJNUMBER with rollup
ORDER BY   GROUPING (PAPROJNUMBER

谁能指出我正确的方向?

4

2 回答 2

2

使用GROUP BY语句时,必须在group by子句中指定不使用聚合的任何内容。

SELECT     AccountNo,  
           TransDate,  
           JournalNo,  
           AuditTrail,  
           Description,  
           JnlAmount,  
           ACTINDX,  
           CONTRACTNAME,  
           PAPROJNUMBER,GROUPING(PAPROJNUMBER) as PAPROJNUMBER,  
           SUM(PAFeebillamount) as total 
FROM       SRVS.dbo.BTQGLDistributionsWithProjectCodesHM 
GROUP BY   
       AccountNo,  
       TransDate,  
       JournalNo,  
       AuditTrail,  
       Description,  
       JnlAmount,  
       ACTINDX,  
       CONTRACTNAME,
       PAPROJNUMBER with rollup 
ORDER BY   GROUPING (PAPROJNUMBER)
于 2012-10-08T10:12:35.043 回答
1

您必须在SELECT语句中包含未包含在GROUP BY子句中的聚合函数中的所有列,如下所示:

SELECT AccountNo, TransDate, 
  JournalNo, AuditTrail, Description, 
  JnlAmount, ACTINDX, CONTRACTNAME, 
  PAPROJNUMBER, GROUPING(PAPROJNUMBER) as PAPROJNUMBER,
  SUM(PAFeebillamount) as total 
FROM SRVS.dbo.BTQGLDistributionsWithProjectCodesHM 
GROUP BY 
  AccountNo, TransDate, 
  JournalNo, AuditTrail, Description, 
  JnlAmount, ACTINDX, CONTRACTNAME, 
  PAPROJNUMBER
with rollup 
ORDER BY GROUPING (PAPROJNUMBER)
于 2012-10-08T10:12:54.023 回答