Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

sql - How to find out duplicate records from multiple tables in MS Access based on two condition

How to find out duplicate records from multiple tables in MS Access based on month and the below columns ? Name, Text, Description, TestDescription

select [table1].[Name], [table1].[Text], [table1].[Description], [table1].[TestDescription] 
From [table1]
UNION ALL 
select  [table2].[Name], [table2].[Text], [table2].[Description], [table2].[TestDescription]
from [table2]
WHERE Table1.month IN ("April","May") and Table2.month IN ("April","May")
group by [table1].[Name], [table1].[Text], [table1].[Description], [table1].[TestDescription]
having count(*) > 1;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Each SELECT in a UNION is it's own seperate query, so you can't do two WHERE clauses at the bottom. Each part must have it's own WHERE and each part doesn't know about the other part (it's out of context). Instead treat the entire UNION result set as a subquery to do your GROUP BY... HAVING test:

SELECT *
FROM
    (
        SELECT [table1].[Name], [table1].[Text], [table1].[Description], [table1].[TestDescription] 
        FROM [table1]
        WHERE Table1.month IN ("April","May") 
        UNION ALL 
        SELECT  [table2].[Name], [table2].[Text], [table2].[Description], [table2].[TestDescription]
        FROM [table2]
        WHERE Table2.month IN ("April","May")
    ) AS myUnion
GROUP BY [myUnion].[Name], [myUnion].[Text], [myUnion].[Description], [myUnion].[TestDescription]
HAVING COUNT(*) > 1;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...