SELECT CASE和GROUP BY无效的列错误

时间:2019-07-18 16:10:49

标签: sql sql-server database group-by case

我在CASE子句中使用了“ A.Observacao”列,但由于它会更改我的结果集,因此不应该将其分组。但是,我收到以下错误:选择列表中的列'crpContaLancamento.Observacao'无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。如何在不更改预期结果的情况下解决此查询?

SELECT SUM(A.valor) as Valor, A.IdLote,
  CASE
    WHEN A.TpHistorico = 9 THEN 
    CASE WHEN A.Observacao like '%Estorno%' THEN 'Estorno de débito do cartão' ELSE 'Descarga de cartão' END
    WHEN A.TpHistorico = 10 THEN 
    CASE WHEN A.Observacao like '%Estorno%' THEN 'Estorno de crédito do cartão' ELSE 'Carga de cartão' END
    END AS TpHistorico
  FROM [crpContaLancamento] A
  where IdClientePJ = 275
  group by A.IdLote, TpHistorico

0 个答案:

没有答案