输入表DEDUPE
SOURCE Legacy_vendor Vendor Condition application Condition type Doc type
MX_PRISMA_PDN 9215217 1012473 EF ZNE1 ZMRO
MX_PRISMA_PDN 9215217 1012473 EF ZNE1 ZNB
MX_PRISMA_PDN 9215217 1012473 EF ZNE1 ZSRM
输出应该
SOURCE Legacy_vendor Vendor Condition application Condition type Doc type
MX_PRISMA_PDN 9215217 1012473 EF ZNE1 ZMRO,ZNB,ZSRM
答案 0 :(得分:0)
BEGIN TRAN
CREATE TABLE #DEDUPE (SOURCE NVARCHAR(50),Legacy_vendor BIGINT,Vendor BIGINT ,[Condition application] NVARCHAR(10),[Condition type] NVARCHAR(20),[Doc type] NVARCHAR(20))
INSERT INTO #DEDUPE
SELECT 'MX_PRISMA_PDN' ,9215217, 1012473,'EF','ZNE1','ZMRO' UNION ALL
SELECT 'MX_PRISMA_PDN', 9215217, 1012473,'EF','ZNE1','ZNB' UNION ALL
SELECT 'MX_PRISMA_PDN', 9215217, 1012473,'EF','ZNE1','ZNB'
SELECT
C.SOURCE,
C.Legacy_vendor,C.Vendor,C.[Condition application],C.[Condition type],
STUFF((
SELECT ', ' + CONVERT(Nvarchar,CP.[Doc type])
FROM
#DEDUPE CP
WHERE
C.SOURCE = CP.SOURCE
FOR XML PATH('')), 1, 2, '') [Doc type]
FROM
#DEDUPE C
GROUP BY C.SOURCE,
C.Legacy_vendor,C.Vendor,C.[Condition application],C.[Condition type]
DROP TABLE #DEDUPE
ROLLBACK TRAN
答案 1 :(得分:-1)
假设SQL Server:您可以使用XML
和Stuff
SELECT DISTINCT SOURCE,Legacy_vendor,Vendor,[Condition application],[Condition type],STUFF((SELECT ','+[Doc type]
FROM Table1 T1
WHERE T1.SOURCE=T2.SOURCE
AND T1.Legacy_vendor=T2.Legacy_vendor
AND T1.Vendor=T2.Vendor
FOR XML PATH('')
), 1, 1, '')
AS [Doc type]
FROM Table1 T2
答案 2 :(得分:-1)
对于SQL Server
select source, legacy_vendor, vendor, condition_application, condition_type,
doctype =
STUFF(( SELECT ' '+ SUB.doctype AS [text()]
FROM DEDUPE SUB
WHERE sub.source = main.source and sub.legacy_vendor = main.legacy_vendor and
sub.vendor = main.vendor and sub.condition_application= main.condition_application and sub.condition_type = main.condition_type
FOR XML PATH('')
), 1, 1,'')
from DEDUPE main
group by source, legacy_vendor, vendor, condition, condition_type
我使用STUFF加入1列
中的所有doc类型答案 3 :(得分:-1)
您没有告诉我们您的DBMS。
对于PostgreSQL,您可以使用string_agg()
select source,
Legacy_vendor,
Vendor,
"Condition application"
"Condition type"
string_agg("Doc type", ',') as doc_type
from the_table
group by source,
Legacy_vendor,
Vendor,
"Condition application"
"Condition type"