distinct

SQL总结(七)查询实战

为君一笑 提交于 2020-01-16 15:01:32
SQL总结(七)查询实战 一、场景 给定一个场景,学生选课系统为例,大家很熟悉。 主要关系: 学生(学号、姓名、年龄、性别) 教师(教师ID,教师姓名) 课程(课程ID,课程名称,任教教师ID) 成绩(学生ID,课程ID,成绩) 二、创建表并预置数据 创建关系表: --学生:Student(SID,SName,SAge,SSex) --学生表(学号、姓名、年龄、性别) --性别,0表示男,1表示女 -- --IF EXISTS(SELECT OBJECT_ID('Student')) /*此处永远为true,原因是OBJECT_ID返回具体ID,或者NULL*/ --使用下列语句,如果没有,什么都不返回,也就不存在 IF EXISTS(SELECT id FROM sysobjects WHERE name='Student') DROP Table Student Create table Student ( SID nvarchar(20) primary key not null, SName nvarchar(20), SAge int, SSex bit ) --教师:Teacher(TID,TName) --教师表(教师ID,教师姓名) IF EXISTS(SELECT id FROM sysobjects WHERE name='Teacher') Drop

How to use distinct and sum both together in oracle?

让人想犯罪 __ 提交于 2020-01-16 12:22:23
问题 For example my table contains the following data: ID price ------------- 1 10 1 10 1 20 2 20 2 20 3 30 3 30 4 5 4 5 4 15 So given the example above, ID price ------------- 1 30 2 20 3 30 4 20 ----------- ID 100 How to write query in oracle? first sum(distinct price) group by id then sum(all price). 回答1: I would be very careful with a data structure like this. First, check that all id s have exactly one price: select id from table t group by id having count(distinct price) > 1; I think the

MySQL 去重方法之一

♀尐吖头ヾ 提交于 2020-01-16 05:57:55
在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。其原因是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

Remove duplicate rows, regardless of new information -PySpark

纵然是瞬间 提交于 2020-01-15 10:15:39
问题 Say I have a dataframe like so: ID Media 1 imgix.com/20830dk 2 imgix.com/202398pwe 3 imgix.com/lvw0923dk 4 imgix.com/082kldcm 4 imgix.com/lks032m 4 imgix.com/903248 I'd like to end up with: ID Media 1 imgix.com/20830dk 2 imgix.com/202398pwe 3 imgix.com/lvw0923dk 4 imgix.com/082kldcm Even though that causes me to lose 2 links for ID = 4, I don't care. Is there a simple way to do this in python/pyspark? 回答1: Group by on col('ID') Use collect_list with agg to aggregate the list Call getItem(0)

Remove duplicate rows, regardless of new information -PySpark

北城余情 提交于 2020-01-15 10:15:07
问题 Say I have a dataframe like so: ID Media 1 imgix.com/20830dk 2 imgix.com/202398pwe 3 imgix.com/lvw0923dk 4 imgix.com/082kldcm 4 imgix.com/lks032m 4 imgix.com/903248 I'd like to end up with: ID Media 1 imgix.com/20830dk 2 imgix.com/202398pwe 3 imgix.com/lvw0923dk 4 imgix.com/082kldcm Even though that causes me to lose 2 links for ID = 4, I don't care. Is there a simple way to do this in python/pyspark? 回答1: Group by on col('ID') Use collect_list with agg to aggregate the list Call getItem(0)

How to do a SELECT DISTINCT equivalent for a page filter in NHibernate?

笑着哭i 提交于 2020-01-15 06:12:08
问题 Lets say you are working in SQL 2005 with a copy of Northwind database installed. Your working on an ASP.NET application with an Employees "browse" page. At the top of the page you have a "Title" filter where you would like to display these 5 choices in a dropdown: [ALL] Vice President, Sales Sales Representative Sales Manager Inside Sales Coordinator In T-SQL you would use something like the statement below to get your list. SELECT DISTINCT Title FROM Employees ORDER BY Title What is the

How to do a SELECT DISTINCT equivalent for a page filter in NHibernate?

纵然是瞬间 提交于 2020-01-15 06:11:09
问题 Lets say you are working in SQL 2005 with a copy of Northwind database installed. Your working on an ASP.NET application with an Employees "browse" page. At the top of the page you have a "Title" filter where you would like to display these 5 choices in a dropdown: [ALL] Vice President, Sales Sales Representative Sales Manager Inside Sales Coordinator In T-SQL you would use something like the statement below to get your list. SELECT DISTINCT Title FROM Employees ORDER BY Title What is the

LINQ - Distinct is ignored?

感情迁移 提交于 2020-01-15 04:35:07
问题 So I have a problem with my LINQ code, where I have to select a Distinct data set, I implement the following IEqualityComparer : public class ProjectRoleComparer : IEqualityComparer<ProjectUserRoleMap> { public bool Equals(ProjectUserRoleMap x, ProjectUserRoleMap y) { return x.RoleID.Equals(y.RoleID); } public int GetHashCode(ProjectUserRoleMap obj) { return obj.GetHashCode(); } } In this context, I wish to retrieve a bunch of ProjectUserRoleMap objects related to a given Project, identified

Return distinct list of object array where number of array items is non-specific

自古美人都是妖i 提交于 2020-01-14 10:46:07
问题 Is there a way using LINQ to get a distinct list of items from a list of object array without knowing how many items exist in each array? The number of items in each array item will be the same throughout the list. // Foo is a list of object arrays. The number of items // each array is non-specific. // (In this example there is only 3 items, but there could be 100) var foo = new List<object[]>(); // I add some items to the list. foo.Add(new object[] { 1, "Something", true }); foo.Add(new

mysql select distinct query in PHP

独自空忆成欢 提交于 2020-01-14 10:33:11
问题 $sql = "SELECT DISTINCT Branch FROM student_main"; $result = mysql_query($sql); $row_num = mysql_num_rows($result); $rows = mysql_fetch_array($result); echo "<select name='Branch'>"; for($i=0;$i<=$row_num-1;$i++){ echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>"; } echo "</select>"; echo "<input type='submit' Value='submit' />"; echo "</form>"; I am trying to create a dropdown using the above code for my form. But its not working. There are 3 distinct values in the Branch column