2012-10-08 29 views
0

我運行下面的SQL,但不斷收到錯誤: -SQL分組錯誤

'Column 'SRVS.dbo.BTQGLDistributionsWithProjectCodesHM.AccountNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

我運行的代碼是: -

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 

任何人都可以點我在正確的方向上這個?

+0

歡迎StackOverflow上:如果您發佈的代碼,XML或數據樣本,**請**在文本編輯器中高亮顯示這些行,並在編輯器工具欄上單擊「代碼樣本」按鈕(「{}」)以精確地格式化和語法突出顯示它!如果您發佈錯誤消息,請**使用blockquotes('「')正確格式化錯誤消息。 –

回答

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) 
1

你必須包括在不包括在GROUP BY子句在聚合函數,像這樣的SELECT語句中的所有列:

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)