选择下面的行可能具有相同数据的行

时间:2016-09-09 18:12:46

标签: ms-access

我有这个:

row  name       company         cusaddr_1
1    Bob        Happy Valley    1 RIVER PARKWAY
2               Happy Valley    1 RIVER PARKWAY
3    Lorie      Pet supply      1 Falls Rd
4    Greg       Pet supply      1 Falls Rd
5               Pet supply      1 Falls Rd
6               A Best Auto     1 camp dr
7    Ron        1 Broadway Ave

我想要的是返回行1,3,4,6,7。基本上,我如何选择公司有名称并且是重复的行,但仍保留第6行的行,其中只有一个公司选项而没有名称?这是Access 2013,我创建了一个像这样的联合查询的表:

select DISTINCT blName as name, company, cusaddr_1
from one_table
union all
select DISTINCT shName as name, company, cusaddr_1
from one_table

1 个答案:

答案 0 :(得分:0)

我会创建一个带有WHERE子句的查询,检查公司名称空白的行是否有另一个带有名称的记录,如果记录的话,则排除记录:

WHERE (name is NOT NULL) or 
((name IS NULL) AND (company IN 
   (SELECT company FROM one_table GROUP BY company HAVING MAX(name) IS NULL)))

如果您只需要为只有空白名称的公司返回一行,那么我将针对行号创建WHERE子句:

WHERE (name is NOT NULL) or 
((name IS NULL) AND (row In 
(SELECT Min(row) FROM one_table GROUP BY company HAVING MAX(name) Is Null)))