2017-02-12 50 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