distinct 多个字段问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了distinct 多个字段问题相关的知识,希望对你有一定的参考价值。

你好,请问最终这个问题如何解决呢?我也遇到这个问题,想再添加说明字段e,但不是分组函数。 http://zhidao.baidu.com/question/147214526.html#
还有是用 oracle,pl/sql

--模拟一下添加字段E
declare @tb table
(
A int,
B int,
C int,
D int,
E nvarchar(30)
)
insert into @tb(A,B,C,D,E)
select 1,2,1,4,'备注1' union all
select 1,2,1,5,'备注2' union all
select 2,3,4,6,'备注3' union all
select 2,3,4,1,'备注4' union all
select 3,1,3,2,'备注5'
--使用相关子查询
select TA.* from @tb TA
where not exists
(
select null from @tb TB where TA.A=TB.A and TA.B=TB.B and TA.C=TB.C and TA.D>TB.D
)
--使用简单子查询
select N.* from @tb N inner join
(
select A,B,C,min(D) D
from @tb
group by A,B,C
) M on (N.A=M.A and N.B=M.B and N.C=M.C and N.D=M.D)
---
结果都是:
A B C D E
-----------------------------------
1 2 1 4 备注1
2 3 4 1 备注4
3 1 3 2 备注5

----
以上两种方法。基本支持Oracle sqlserver DB2等,建表语句不兼容追问

使用简单子查询
好像还是不行。。。
现在就是想添加一个说明字段e。。。

追答

在内层使用分组业务,外层添加E。
--------------------------------------
如果你的字段E,是分组KEY,那就要写入 group by 分组KEY
---
select N.A,N.B,N.C,N.D,N.E from @tb N inner join
(
select A,B,C,min(D) D
from @tb
group by A,B,C
) M on (N.A=M.A and N.B=M.B and N.C=M.C and N.D=M.D)
--如果加入E出现(A,B,C,E)-->元组 函数约束
--
select N.A,N.B,N.C,N.D,N.E from @tb N inner join
(
select A,B,C,E,min(D) D
from @tb
group by A,B,C,E
) M on (N.A=M.A and N.B=M.B and N.C=M.C and N.D=M.D and N.E=M.E)
--
如果函数约束没有变,无须修改。

参考技术A select a,b,c,d,e
from (select a,b,c,d,e
,row_number() over(partition by a,b,c order by a) row_num
from table_name) tn
where tn.row_num = 1追问

呃,没看懂。。。
我之前写过
select distinct a,b,c,min(d)
from 表名
group by a,b,c;
这样就可以显示出来。
但现在要添加字段e,就没法了,连接自身也没法。

追答

你测试一下就知道是怎么回事了。row_number() over(partition by a,b,c order by a) 根据a,b,c字段分组,然后把a,b,c有相同重复的只取一条记录。

参考技术B select a,b,c, first_value(d) over(partition by a,b,c order by d) d,e from table

mysql中去重 distinct 用法

distinct的使用语法是这样的:

select distinct expression[,expression...] from tables [where conditions];


在使用distinct的过程中主要注意一下几点:

  • 在对字段进行去重的时候,要保证distinct在所有字段的最前面
  • 如果distinct关键字后面有多个字段时,则会对多个字段进行组合去重,只有多个字段组合起来的值是相等的才会被去重

下面我们通过在开发过程中经常遇到的一些关于distinct的实例来加深大家对该关键字用法的理解:

数据库表结构和数据如下图所示:

  •  对单个字段进行去重sql:
select distinct  age from user;

查询结果
age
10
20
30
  • 对多个字段进行去重sql:
select distinct name,age from user;

查询结果
name    age
One    10
Zero    20
Two    20
Four    30
One    30
  • 对多个字段进行去重并求count的sql(实际中我们往往用distinct来返回不重复字段的条数(count(distinct id)),其原因是distinct只能返回他的目标字段,而无法返回其他字段):
select count(distinct name,age) as total from user;

查询结果
total
5
  • 对select * 进行去重
select distinct * from user;

由于 * 代表所有字段,所以该sql和 select distinct id,name,age,sign from user 语义相同

查询结果:
id        name    age        sign
1        One        10        梦想要有的,万一实现了呢
2        Zero    20        http://www.chaoshizhushou.com
3        Two        20        OneZeroTwoFour
4        Four    30        加油
5        One        30        学习才是硬道理
6        Four    30        一日三省吾身

 

如果sql这样写:select id,distinct name from user,这样mysql会报错,因为distinct必须放在要查询字段的开头。

所以一般distinct用来查询不重复记录的条数。

如果要查询不重复的记录,有时候可以用group by :

select id,name from user group by name;

以上是关于distinct 多个字段问题的主要内容,如果未能解决你的问题,请参考以下文章

ORACLE中的distinct消除重复行,我用where链接了多个表 查询了多个字段

Oracle-count(distinct( 字段A || 字段B)) distinct多个字段

用distinct 怎样根据两个字段找唯一条纪录

mysql distinct多个字段怎么用

mysql中去重 distinct 用法

MongoDB distinct() 指定字段去重