SQL映射来自另一个表的重复条目

时间:2017-10-17 08:15:05

标签: sql sql-server duplicates

SQL中是否有一种方法可以映射重复的条目并从另一个表中获取数据。我需要在表2中映射原始文件名,但数据来自表1并使用新文件名作为映射引用?我试图用分区做ROW_NUMBER,但我不会去任何地方

表1:

new filename       original filename
text_001.pdf       test1333.pdf 
text_001.pdf       test4443.pdf
hello2332.pdf      world1234.txt
hello2332.pdf      world3331.txt

表2:

EE       new filename       
00001    text_001.pdf      
00001    text_001.pdf
00002    hello2332.pdf 
00002    hello2332.pdf  

输出表:

    EE       new filename       original filename(entries coming from table 1)
    00001    text_001.pdf       test1333.pdf 
    00001    text_001.pdf       test4443.pdf
    00002    hello2332.pdf      world1234.txt
    000002   hello2332.pdf      world3331.txt

1 个答案:

答案 0 :(得分:1)

;WITH xx AS
(SELECT DISTINCT
[EE],
[new filename]
FROM [Table 2]
)
SELECT 
xx.[EE],
xx.[new filename],
a.[original filename]
FROM xx 
LEFT JOIN [table 1] as a
ON a.[new filename] = xx.[new filename]
相关问题