分组结果时Concat而不是SUM [英] Concat instead of SUM when grouping results

查看:72
本文介绍了分组结果时Concat而不是SUM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个非常简单的问题,我将用一个例子来说明:


我在我的记录中有以下记录桌子:

A 1 C

A 2 C

3 C

B 8 K

B 9 K


我现在想把它们分组,结果必须是:

A 1,2,3 C
B 8,9 K


因此第二行的结果必须连接在一起。我想

没有这个功能......最简单的解决办法是什么?


亲切的问候,


Bart Warnez

Hello,

I have a very simple problem which I will illustrate with an example:

I have the following records in my table:
A 1 C
A 2 C
A 3 C
B 8 K
B 9 K

I now want to group them and the result has to be:
A 1,2,3 C
B 8,9 K

So the results in the second row have to be concatenated. I guess
there is no function to do this... What is the simplest solution?

Kind regards,

Bart Warnez

推荐答案

嗨巴特,


我看到这个问题得到了很好的回答整齐地说,所以有一点点b / b
挖掘和一些复制/粘贴我想出了:

CREATE TABLE test(test1 VARCHAR(5),test2 varchar(5),test3

varchar(5))

INSERT INTO test(test1,test2,test3)

SELECT ''A'','''',''C''

UNION ALL

SELECT''''',''''''''' C''

UNION ALL

SELECT''A'','''',''C''

UNION ALL

SELECT''''','''''''''''''''''''''''''''''''''$ $ $ $ $ $ $ $ B $' ''9'',''C''


SELECT test1,SUBSTRING((选择'',''+ test2 as [text()]
来自测试t

其中t.test1 = ot.test1

for xml path(''''),elements),3,100 )作为test2,test3

来自测试ot

GROUP BY test1,test3


DROP TABLE测试


似乎有效:)


祝你好运!

J
Hi Bart,

I''ve seen this question answered very neatly before, so with a bit of
digging and some copy/paste I came up with:

CREATE TABLE test (test1 VARCHAR(5), test2 varchar(5), test3
varchar(5))

INSERT INTO test(test1, test2, test3)
SELECT ''A'', ''1'', ''C''
UNION ALL
SELECT ''A'', ''2'', ''C''
UNION ALL
SELECT ''A'', ''3'', ''C''
UNION ALL
SELECT ''B'', ''8'', ''C''
UNION ALL
SELECT ''B'', ''9'', ''C''

SELECT test1, SUBSTRING((select '', '' + test2 as [text()]
from test t
where t.test1 = ot.test1
for xml path(''''), elements), 3, 100) as test2, test3
FROM test ot
GROUP BY test1, test3

DROP TABLE test

which seems to work :)

Good luck!
J


23月11日,12:52,jhofm ... @ googlemail.com写道:
On 23 nov, 12:52, jhofm...@googlemail.com wrote:

嗨巴特,


我之前已经看到这个问题得到了非常巧妙的回答,所以我想了一下

挖掘和一些复制/粘贴我想出了:


CREATE TABLE test(test1 VARCHAR(5),test2 varchar(5),test3

varchar(5))

INSERT INTO test(test1,test2,test3)

SELECT''A'',''1',''C''

UNION ALL

SELECT''A'' ,'''',''C''

UNION ALL

选择''A'','''',''C''

UNION ALL

SELECT''''','''''''''''''''''''''''''''''''''$ $ $ $ $ $ $ $ B $' ''9'',''C''


SELECT test1,SUBSTRING((选择'',''+ test2 as [text()]

来自测试t

其中t.test1 = ot.test1

表示xml路径('''),元素),3,100)为test2,test3

FROM测试ot

GROUP BY test1,test3


DROP TABLE测试


这似乎有效:)


祝你好运!

J
Hi Bart,

I''ve seen this question answered very neatly before, so with a bit of
digging and some copy/paste I came up with:

CREATE TABLE test (test1 VARCHAR(5), test2 varchar(5), test3
varchar(5))

INSERT INTO test(test1, test2, test3)
SELECT ''A'', ''1'', ''C''
UNION ALL
SELECT ''A'', ''2'', ''C''
UNION ALL
SELECT ''A'', ''3'', ''C''
UNION ALL
SELECT ''B'', ''8'', ''C''
UNION ALL
SELECT ''B'', ''9'', ''C''

SELECT test1, SUBSTRING((select '', '' + test2 as [text()]
from test t
where t.test1 = ot.test1
for xml path(''''), elements), 3, 100) as test2, test3
FROM test ot
GROUP BY test1, test3

DROP TABLE test

which seems to work :)

Good luck!
J



嘿,谢谢非常,它工作:)。唯一的问题是它需要超过10秒来执行它并且只有5条记录:(。


亲切的问候,


Bart

Hey, thank you very much, it works :). The only problem is that it
lasts more than 10 s to execute it and that with only 5 records :(.

Kind Regards,

Bart


我也试过下面的解决方案(使用相同的测试表),

with一个函数。但是响应时间又很慢......


创建函数dbo.fn_groupIt(@ test1 varchar(5),@ test3 varchar(5))

返回varchar(5000)

as

开始

声明@out varchar(5000)

选择@out = coalesce(@out +'',''+ convert(varchar,test2),

convert(varchar,test2))

来自test

其中test1 = @ test1和

test3 = @ test3


返回@out

end


选择test1,dbo.fn_groupIt(test1,test3)test2,test3

来自(

select test1,test3

来自测试

group by test1,test3

)a
I have also tried out the solution below (with the same test-table),
with a function. But again the response time is very slow...

create function dbo.fn_groupIt(@test1 varchar(5),@test3 varchar(5))
returns varchar(5000)
as
begin
declare @out varchar(5000)
select @out = coalesce(@out + '','' + convert(varchar,test2),
convert(varchar,test2))
from test
where test1 = @test1 and
test3 = @test3

return @out
end

select test1, dbo.fn_groupIt(test1,test3) test2,test3
from (
select test1,test3
from test
group by test1,test3
) a


这篇关于分组结果时Concat而不是SUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆