2013-07-31 16 views
1

我試圖按日期(mm/yyyy)對錶進行分組,然後按invoicetype進行分組,我嘗試將代碼放在下面的代碼中,但我一直在。錯誤 '無效的列日'SQl,多個問題(加入參數並將一列放入一行

SELECT RIGHT(CONVERT(VARCHAR(10), case_createddate, 103), 7) AS Date, 
     case_invoicetype, 
     Sum(case_totalexvat) 
FROM cases AS ca 
WHERE case_primaryCompanyid = 1111 
GROUP BY ca.Date, 
      case_invoicetype 

我嘗試這樣做:

group by YEAR(case_createddate), MONTH(case_createddate) 

但我得到的錯誤:

case_createddate is invalid in the select list because it is not contained in either an aggregate function or the group by clause

有任何想法嗎 ?

感謝

回答

0

您需要通過子句包括從選擇的組中的所有非聚合列。 ca.Date是不是在選擇,我懷疑你想這樣的:

SELECT RIGHT(CONVERT(VARCHAR(10), case_createddate, 103), 7) AS Date, 
     case_invoicetype, 
     Sum(case_totalexvat) 
FROM cases AS ca 
WHERE case_primaryCompanyid = 1111 
GROUP BY RIGHT(CONVERT(VARCHAR(10), case_createddate, 103), 7), 
      case_invoicetype 
+0

,是完美的,太感謝你了, – Rachsherry

0

使用GROUP BY子句對整個表達式

SELECT RIGHT(CONVERT(VARCHAR(10), case_createddate, 103), 7) AS Date, 
     case_invoicetype, 
     Sum(case_totalexvat) 
FROM cases AS ca 
WHERE case_primaryCompanyid = 1111 
GROUP BY RIGHT(CONVERT(VARCHAR(10), case_createddate, 103), 7), 
      case_invoicetype 
+0

感謝亞歷山大,是固定的,只是不是肯定的語法 – Rachsherry