2017-02-12 1 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 절에있는 텍스트를 포함하지 않을이 오류를 얻고 예 즉, 쿼리 실행을하게하지만 문제는 내가 그것을 원하지 않는 분야에서 다른 텍스트가있다 그룹화는 CASE

과 일치하는 항목의 텍스트와 이름을 바꿔서 그룹화 할 때이 결과가 표시됩니다.

  43036 SPECIAL  73.0000 
      43036 SPECIAL  6.0000 
+1

나도 몰라' – dnoeth

+0

을 StockItems.Description' 이것은 왜 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