2017-02-12 8 views
0

SQL Serverを使用しています。次の問題があります。私は、このエラーにSQL CASE文を使用してTextを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句内のテキストを含める必要はありませんはい、それは、クエリの実行になりますが、問題は、私はそれを望んでいない分野の他のテキストがあります私がグループ化するのは、CASEに一致する項目の名前をTextに置き換えることです。

私はこの結果を得てグループにテキストを追加します。

  43036 SPECIAL  73.0000 
      43036 SPECIAL  6.0000 
+1

私は、これはあなたが実際に何をしたいかどうかを知り、代わりStockItems.Description' – dnoeth

+0

'の、GROUP BYでCASEを繰り返しませんなぜこれは 'mysql'でタグ付けされていますか? – shmosel

答えて

2

問題はエラーと全く同じです。 group by句に含まれていないTransactionsLine.textを選択しています。

あなたはおそらくあなたの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 
関連する問題