distinct

Want to remove duplicated rows unless NA value exists in columns

血红的双手。 提交于 2020-01-30 08:38:06
问题 I have a data table with 4 columns: ID, Name, Rate1, Rate2. I want to remove duplicates where ID, Rate1, and Rate 2 are the same, but if they are both NA, I would like to keep both rows. Basically, I want to conditionally remove duplicates, but only if the conditions != NA. For example, I would like this: ID Name Rate1 Rate2 1 Xyz 1 2 1 Abc 1 2 2 Def NA NA 2 Lmn NA NA 3 Hij 3 5 3 Qrs 3 7 to become this: ID Name Rate1 Rate2 1 Xyz 1 2 2 Def NA NA 2 Lmn NA NA 3 Hij 3 5 3 Qrs 3 7 Thanks in

MongoDB中的聚合操作

柔情痞子 提交于 2020-01-28 18:44:26
根据MongoDB的文档描述,在MongoDB的聚合操作中,有以下五个聚合命令。 其中,count、distinct和group会提供很基本的功能,至于其他的高级聚合功能(sum、average、max、min),就需要通过mapReduce来实现了。 在MongoDB2.2版本以后,引入了新的聚合框架(聚合管道,aggregation pipeline ,使用aggregate命令),是一种基于管道概念的数据聚合操作。 Name Description count Counts the number of documents in a collection. distinct Displays the distinct values found for a specified key in a collection. group Groups documents in a collection by the specified key and performs simple aggregation. mapReduce Performs map-reduce aggregation for large data sets. aggregate Performs aggregation tasks such as group using the aggregation

mysql去重的最方便的两种方法

左心房为你撑大大i 提交于 2020-01-25 23:47:08
参考资料:http://blog.csdn.net/guocuifang655/article/details/3993612 方法一: 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。其原因是 distinct只能返回它的目标字段,而无法返回其它字段 下面先来看看例子: table id name 1 a 2 b 3 c 4 c 5 b 库结构大概这样,这只是一个简单的例子,实际情况会复杂得多。 比如我想用一条语句查询得到name不重复的所有数据,那就必须使用distinct去掉多余的重复记录。 select distinct name from table 得到的结果是: name a b c 好像达到效果了,可是,我想要得到的是id值呢?改一下查询语句吧: select distinct name, id from table 结果会是: id name 1 a 2 b 3 c 4 c 5 b distinct怎么没起作用?作用是起了的,不过他同时作用了两个字段,也就是必须得id与name都相同的才会被排除。。。。。。。 我们再改改查询语句: select id, distinct name from table

HOW TO: SQL Server select distinct field based on max value in other field

故事扮演 提交于 2020-01-24 19:14:41
问题 id tmpname date_used tkt_nr ---|---------|------------------|--------| 1 | template| 04/03/2009 16:10 | 00011 | 2 | templat1| 04/03/2009 16:11 | 00011 | 5 | templat2| 04/03/2009 16:12 | 00011 | 3 | diffname| 03/03/2009 15:11 | 00022 | 4 | diffname| 03/03/2009 16:12 | 00022 | 6 | another | 03/03/2009 16:13 | NULL | 7 | somethin| 24/12/2008 11:12 | 00023 | 8 | name | 01/01/2009 12:12 | 00026 | I would like to have the result: id tmpname date_used tkt_nr ---|---------|------------------|--------

HOW TO: SQL Server select distinct field based on max value in other field

ぐ巨炮叔叔 提交于 2020-01-24 19:13:25
问题 id tmpname date_used tkt_nr ---|---------|------------------|--------| 1 | template| 04/03/2009 16:10 | 00011 | 2 | templat1| 04/03/2009 16:11 | 00011 | 5 | templat2| 04/03/2009 16:12 | 00011 | 3 | diffname| 03/03/2009 15:11 | 00022 | 4 | diffname| 03/03/2009 16:12 | 00022 | 6 | another | 03/03/2009 16:13 | NULL | 7 | somethin| 24/12/2008 11:12 | 00023 | 8 | name | 01/01/2009 12:12 | 00026 | I would like to have the result: id tmpname date_used tkt_nr ---|---------|------------------|--------

SPOJ - DISUBSTR Distinct Substrings(后缀数组)

假如想象 提交于 2020-01-24 16:09:20
题目链接: 点击查看 题目大意:给出一个字符串,求出本质不同的字串的数量 题目分析:正难则反,我们可以先求出总的字串有 n*(n+1)/2 个,然后遍历一遍height数组减去所有重复的部分就是答案了 代码: #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<cmath> #include<sstream> using namespace std; typedef long long LL; const int inf=0x3f3f3f3f; const int N=1e5+100; char str[N]; int sa[N]; //SA数组,表示将S的n个后缀从小到大排序后把排好序的 //的后缀的开头位置顺次放入SA中 int t1[N],t2[N],c[N]; int rk[N],height[N],len; int s[N]; void build_sa(int s[],int n,int m)//n为添加0后的总长 { int i,j,p,*x=t1,*y=t2; for(i

SQL去重distinct方法解析

孤人 提交于 2020-01-24 14:37:34
一 distinct 含义: distinct 用来查询不重复记录的条数 , 即 distinct 来返回不重复字段的条数( count(distinct id) ) , 其原因是 distinct 只能返回他的目标字段,而无法返回其他字段 用法注意: 1.distinct 【查询字段】, 必须放在要查询字段的开头 ,即放在第一个参数; 2. 只能在 SELECT 语句中使用,不能在 INSERT, DELETE, UPDATE 中使用; 3.DISTINCT 表示对后面的所有参数的拼接取 不重复的记录,即查出的参数拼接每行记录都是唯一的 4.不能与all同时使用,默认情况下,查询时返回的就是所有的结果。 1.1只对一个字段查重 对一个字段查重,表示选取该字段一列不重复的数据。 示例表: psur_list PLAN_NUMBER字段去重, 语句:SELECT DISTINCT PLAN_NUMBER FROM psur_list; 结果如下: 1.2多个字段去重 对多个字段去重,表示选取多个字段拼接的一条记录,不重复的所有记录 示例表: psur_list PLAN_NUMBER和PRODUCT_NAME字段去重, 语句:SELECT DISTINCT PLAN_NUMBER,PRODUCT_NAME FROM psur_list; 结果如下: 期望结果:只对第一个参数PLAN

[LeetCode] 694. Number of Distinct Islands

好久不见. 提交于 2020-01-24 04:15:13
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1 's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct islands. An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other. Example 1: 11000 11000 00011 00011 Given the above grid map, return 1 . Example 2: 11011 10000 00001 11011 Given the above grid map, return 3 . Notice that: 11 1 and 1 11 are considered different

mysql中去重 distinct 用法

眉间皱痕 提交于 2020-01-23 11:16:34
在使用mysql时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count(distinct id)),其原因是distinct只能返回他的目标字段,而无法返回其他字段,例如有如下表user: 用distinct来返回不重复的用户名:select distinct name from user;,结果为: 这样只把不重复的用户名查询出来了,但是用户的id,并没有被查询出来:select distinct name,id from user;,这样的结果为: distinct name,id 这样的mysql 会认为要过滤掉name和id两个字段都重复的记录,如果sql这样写:select id,distinct name from user,这样mysql会报错,因为distinct必须放在要查询字段的开头。 所以一般distinct用来查询不重复记录的条数。 如果要查询不重复的记录,有时候可以用group by : select id,name from user group by name; 来源: https://www.cnblogs.com/lxwphp/p/11339949.html

Substring with At Least K Distinct Characters

守給你的承諾、 提交于 2020-01-23 09:29:18
Given a string S with only lowercase characters. Return the number of substrings that contains at least k distinct characters. Example Example 1: Input: S = "abcabcabca", k = 4 Output: 0 Explanation: There are only three distinct characters in the string. Example 2: Input: S = "abcabcabcabc", k = 3 Output: 55 Explanation: Any substring whose length is not smaller than 3 contains a, b, c. For example, there are 10 substrings whose length are 3, "abc", "bca", "cab" ... "abc" There are 9 substrings whose length are 4, "abca", "bcab", "cabc" ... "cabc" ... There is 1 substring whose length is 12,