我目前正在尝试汇总一些数据,以便日后导出到1条记录,其中每条记录的LookUpID是相同的
application_id card_holder_name transaction_id LookUpID
1060693 Bob 6247460 765858xxxxxx9999_2017
964851 James 6259094 765858xxxxxx9999_2017
937321 Fred 6262332 765858xxxxxx9999_2017
在这种情况下,应该有一条记录,但应该汇总application_id,card_holder_name,transaction_id的数据。每个元素将由回车符分隔,但仍然相互关联。
E.G。
LookUpID Application_ID Card_Holder_Name Transaction_ID
765858xxxxxx9999_2017 1060693 964851 937321 Bob James Fred 6247460 6259094 6262332
下面的代码在查看application_id时工作正常,但在添加card_holder_name和tranaction_id时,它们不是彼此属于的代码。我没想到它会像复制代码一样容易,这正是我最初的做法。任何人都可以帮忙,代码如下所示。
create table #test (application_id int, card_holder_name varchar(50), transaction_id int, LookUpID nvarchar(50))
insert into #test (application_id, card_holder_name, transaction_id, LookUpID)
select 1060693, 'Bob', 6247460, '765858xxxxxx9999_2017'
union all
select 964851, 'James', 6259094, '765858xxxxxx9999_2017'
union all
select 937321, 'Fred', 6262332, '765858xxxxxx9999_2017'
select LookUpID,
stuff((SELECT distinct CHAR(13)+CHAR(10) + cast(application_id as varchar(10))
FROM #test t2
where t2.LookUpID = t1.LookUpID
FOR XML PATH(''), TYPE).value('.','nvarchar(max)'),1,2,'') as Application_ID,
stuff((SELECT distinct CHAR(13)+CHAR(10) + cast(transaction_id as varchar(10))
FROM #test t2
where t2.LookUpID = t1.LookUpID
FOR XML PATH(''), TYPE).value('.','nvarchar(max)'),1,2,'') as Transaction_ID,
stuff((SELECT distinct CHAR(13)+CHAR(10) + cast(card_holder_name as varchar(10))
FROM #test t2
where t2.LookUpID = t1.LookUpID
FOR XML PATH(''), TYPE).value('.','nvarchar(max)'),1,2,'') as Card_Holder_Name
from #test t1 where LookUpID = '765858xxxxxx9999_2017'
group by LookUpID
我得到的结果是
LookUpID Application_ID Transaction_ID Card_Holder_Name
765858xxxxxx9999_2017 1060693 937231 964851 6247460 6259094 6262332 Bob Fred James
由于
答案 0 :(得分:0)
这里有一点信念,但如果你的意思是你希望每个列的连接值的顺序相同,则需要在每种情况下添加一个显式的ORDER BY
子句。但是,您需要删除DISTINCT
才能执行此操作,因为您无法GROUP BY
一列和ORDER BY
另一列(按列值的顺序可能存在于多个组中,因此订单无法维护)。所以你的查询需要是:
create table #test (application_id int, card_holder_name varchar(50), transaction_id int, LookUpID nvarchar(50))
insert into #test (application_id, card_holder_name, transaction_id, LookUpID)
select 1060693, 'Bob', 6247460, '765858xxxxxx9999_2017'
union all
select 964851, 'James', 6259094, '765858xxxxxx9999_2017'
union all
select 937321, 'Fred', 6262332, '765858xxxxxx9999_2017'
union all
select 937320, 'Pete', 600, '765858xxxxxx9998_2017'
union all
select 937322, 'Darren', 601, '765858xxxxxx9998_2017'
select LookUpID,
stuff((SELECT CHAR(13)+CHAR(10) + cast(application_id as varchar(10))
FROM #test t2
where t2.LookUpID = t1.LookUpID
order by application_id
FOR XML PATH(''), TYPE).value('.','nvarchar(max)'),1,2,'') as Application_ID,
stuff((SELECT CHAR(13)+CHAR(10) + cast(transaction_id as varchar(10))
FROM #test t2
where t2.LookUpID = t1.LookUpID
order by application_id
FOR XML PATH(''), TYPE).value('.','nvarchar(max)'),1,2,'') as Transaction_ID,
stuff((SELECT CHAR(13)+CHAR(10) + cast(card_holder_name as varchar(10))
FROM #test t2
where t2.LookUpID = t1.LookUpID
order by application_id
FOR XML PATH(''), TYPE).value('.','nvarchar(max)'),1,2,'') as Card_Holder_Name
from #test t1
group by LookUpID