Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Access 2010-在三列中的两列上选择Distinct_Sql_Ms Access - Fatal编程技术网

Sql Access 2010-在三列中的两列上选择Distinct

Sql Access 2010-在三列中的两列上选择Distinct,sql,ms-access,Sql,Ms Access,ACCESS 2010-我在一个表中有3列,希望选择在第1列(c1)中重复但在第2列(c2)和第3列(c3)中必须唯一的记录 理想结果: Query Results c1 c2 c3 1 bacct1 user1 log1 2 bacct1 user2 log2 3 bacct1 user3 log3 不应提取记录4到7,到目前为止,我能够获得理想结果的唯一方法是执行查询(1),首

ACCESS 2010-我在一个表中有3列,希望选择在第1列(c1)中重复但在第2列(c2)和第3列(c3)中必须唯一的记录

理想结果:

Query Results
   c1          c2          c3
1  bacct1      user1       log1
2  bacct1      user2       log2
3  bacct1      user3       log3

不应提取记录4到7,到目前为止,我能够获得理想结果的唯一方法是执行查询(1),首先查找c1的重复项,然后将这些结果分组到另一个查询(2),然后重复查询1和查询2数次,直到我缩小了结果的范围,但我希望找到一个更优雅的解决方案。

表和数据;这部分你应该自己做

create table table1 (
  id int primary key, 
  c1 char(6) not null, 
  c2 char(5) not null, 
  c3 char(4) not null
);

insert into table1 values
(1, 'bacct1', 'user1', 'log1'),
(2, 'bacct1', 'user2', 'log2'),
(3, 'bacct1', 'user3', 'log3'),
(4, 'bacct2', 'user4', 'log4'),
(5, 'bacct2', 'user4', 'log5'),
(6, 'bacct3', 'user6', 'log6'),
(7, 'bacct3', 'user7', 'log6');
听起来你在寻找类似的东西

select t1.id, t1.c1, t1.c2, t1.c3
from table1 t1
inner join 
    (select c1, count(c1)
     from table1
     group by c1
     having count(c1) > 1) t2
on t1.c1 = t2.c1
inner join 
    (select c2, count(c2)
     from table1
     group by c2
     having count(c2) = 1) t3
on t1.c2 = t3.c2
inner join 
    (select c3, count(c3)
     from table1
     group by c3
     having count(c3) = 1) t4
on t1.c3 = t4.c3

ID   C1      C2     C3
--
1    bacct1  user1  log1
2    bacct1  user2  log2
3    bacct1  user3  log3

阅读您的评论后,这可能更接近您的需要。我用标准SQL写了这个。对于Access,您需要删除注释并为内部SELECT语句添加参数

select t1.id, t1.c1, t1.c2, t1.c3
from table1 t1
inner join 
    (-- Duplicated values in c1
     select c1, count(*)
     from table1
     group by c1
     having count(*) > 1 ) t2
on t1.c1 = t2.c1
inner join 
    (-- Unique combinations of c1 and c2
     select c1, c2, count(*)
     from table1
     group by c1, c2
     having count(c2) = 1 ) t3
on t1.c1 = t3.c1 and t1.c2 = t3.c2
inner join 
    (-- Unique combinations of c1 and c3
     select c1, c3, count(*)
     from table1
     group by c1, c3
     having count(*) = 1 ) t4
on t1.c1 = t4.c1 and t1.c3 = t4.c3

“记录4到记录7不应该被提取…”为什么?因为有两行带有“user4”和两行带有“log6”?出于我的目的,记录1-3被认为是唯一的。记录4和5被认为是相同的记录和记录6和7被认为是相同的。我很高兴听到你认为他们相同的记录。你介意解释一下原因吗?因为c2或c3与c1的关系,它们被认为是相同的。记录4与记录5相同,因为c1+c2的组合对于两者都是相同的,即使c3不同。记录6和7是相同的,因为c1+c3的组合是相同的,即使c2不同。基本上,我每天都有重复的原始数据,我需要看到记录1-3的出现。这可能会使事情变得更清楚或更复杂,对不起。我编辑了我的答案。更改可能更接近您要查找的内容。您是否还需要第三个内部联接来仅获取
count(c1)>1
处的值,因为您要在列
c1
中查找重复项?@sigil:是的,我会的。谢谢你指出这一点。这个解决方案绝对是我想要走的方向,并且在很大程度上提供了我所寻求的结果。目前大约有60条记录由于任何原因没有被提取出来,我正在回顾我的数据以找出原因。对于计划将此解决方案用于Access 2010的任何人来说,需要注意的一点是,多个内部联接必须嵌套在括号内。
select t1.id, t1.c1, t1.c2, t1.c3
from table1 t1
inner join 
    (-- Duplicated values in c1
     select c1, count(*)
     from table1
     group by c1
     having count(*) > 1 ) t2
on t1.c1 = t2.c1
inner join 
    (-- Unique combinations of c1 and c2
     select c1, c2, count(*)
     from table1
     group by c1, c2
     having count(c2) = 1 ) t3
on t1.c1 = t3.c1 and t1.c2 = t3.c2
inner join 
    (-- Unique combinations of c1 and c3
     select c1, c3, count(*)
     from table1
     group by c1, c3
     having count(*) = 1 ) t4
on t1.c1 = t4.c1 and t1.c3 = t4.c3