2017-02-12 151 views
0

我正在使用SQL Server,我遇到以下問題,我正在跳一個人可以幫助我。使用SQL CASE語句來替換GROUP BY

我收到此錯誤

Column 'TransactionsLine.Text' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 

我不想包含在GROUP文本BY子句是,使查詢運行,但問題是沒有在該領域的其他文字,我不希望它分組我只想用文本替換名稱與文本匹配的案件

當我添加文本到我得到這個結果的組。

  43036 SPECIAL  73.0000 
      43036 SPECIAL  6.0000 
+1

我不知道這是不是你真正想要的,但重複組中的情況下,通過代替'StockItems.Description' – dnoeth

+0

爲什麼用'mysql'標記? – shmosel

回答

2

問題正是錯誤所說的。您正在選擇TransactionsLine.text,這不在group by子句中。

你可能要放案在group by條款:

select StockItemCode as CODE, 
    (
     case 
      when StockItems.Description like 'item%' 
       then TransactionsLine.text 
      else StockItems.Description 
      end 
     ) as name, 
    SUM(ItemQuantity) as Sales 
from Transactions 
inner join TransactionsLine on Transactions.id = TransactionsLine.TransactionID 
inner join StockItems on TransactionsLine.StockItemID = StockItems.id 
where location = @location 
    and Department = 43 
    and Transactions.date between @FROM 
     and @TO 
    and TransactionTypeID in (3, 32) 
group by StockItemCode, 
    case 
     when StockItems.Description like 'item%' 
      then TransactionsLine.text 
     else StockItems.Description 
     end