【发布时间】:2014-08-13 13:38:58
【问题描述】:
是否可以在下面的语句中为'a','b','test','123','blabla' 创建一些枚举?
sum(case when col1 in ('a','b','test','123','blabla') then col2 end) as sum
我尝试从letters_table 读取它,如下所示:
sum(case when col1 in (select letter from letters_table) then col2 end) as sum
但它告诉我Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
以下对我来说效果不佳:
DECLARE @letters varchar(10)
select @letters = letter FROM letters_table
sum(case when col1 in (@letters) then col2 end) as sum
因为当我打印@letters 时,只有最后一个'blabla'
【问题讨论】:
-
Sum 是一个数学函数,不适用于文本。也许你想使用一个字符串...我相信SQL服务器使用STUFF。但可能需要与 FOR XML Path 一起使用来聚合行。
-
从您的代码中不清楚您要做什么。你指的 col1 和 col2 是什么?你想得到多少金额?
-
@xQbert: 不仅仅适用于这个例子,它可以是任何字符串,不仅仅是 a、b、c、d,而是例如测试、树、心、123等
-
@chipmunkofdoom2: stackoverflow.com/questions/25286039/…
-
@gaffcz 看看stackoverflow.com/questions/12559551/…
标签: sql sql-server