目前我正在試圖推出了一些數據,在以後的日子遠銷1分的記錄,其中lookupId所爲每個記錄是相同的TSQL彙總不相互引用
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
感謝
沒有足夠的信息來幫助你。發佈您試圖包含card_holder_name和Transaction_ID的代碼,併發布結果。我不知道「他們不是屬於對方的」是什麼意思。 –
已經添加如標籤 – PJD
建議的進一步的信息代碼似乎工作正常。結果集究竟有什麼錯誤? – strickt01