为两列和另一列中的任何列选择distinct [英] Select distinct for two columns and any in another column
本文介绍了为两列和另一列中的任何列选择distinct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似下表的sql表
id visit_ date 名称
123 2015年11月6日 3:17:22 PM abc
123 11/6 / 2015 3:24:44 PM abc
456 11/6/2015 3:26:34 PM def
456 11/6/2015 3:30:34 PM def
我需要收到的是
id visit_ date 名称
677930136v 2015年11月6日 3:24:44 PM abc
906080275v 11/6/2015 3:30:34 PM def
我使用了DISTINCT id,访问日期,名称,但它给出了所有结果。如何选择不同的id和name值以及相关的访问日期?
解决方案
尝试GROUP BY
:
SELECT
id,
visit_date,
Max(Name) As 名称
FROM
YourTable
GROUP BY
id,
visit_date
;
I have a my sql table like the table below
id visit_ date Name
123 11/6/2015 3:17:22 PM abc
123 11/6/2015 3:24:44 PM abc
456 11/6/2015 3:26:34 PM def
456 11/6/2015 3:30:34 PM def
What I need to receive is
id visit_ date Name
677930136v 11/6/2015 3:24:44 PM abc
906080275v 11/6/2015 3:30:34 PM def
I have used DISTINCT id , visit date, name but it gives all the results.How can I select distinct values of id and name and the relevant visit date?
解决方案
TryGROUP BY
:
SELECT id, visit_date, Max(Name) As Name FROM YourTable GROUP BY id, visit_date ;
这篇关于为两列和另一列中的任何列选择distinct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文