distinct

How to select part of a Timestamp in a SQL Query

冷暖自知 提交于 2019-12-06 22:12:11
问题 In the DB I am working with, I want to select only the year from a specific TimeStamp field. In particular, I'm hoping to select the unique years from this database column. For instance, if all of the timestamps in the field "ModifyTimeStamp" are either from the year 2002 or the year 2006, I would like returned simply a result of '2002' and '2006'. If this is impossible, I'd be content with getting a result of a bunch of '2002's mixed with '2006's and would parse it later. All I've been able

Using .Select and .Where in a single LINQ statement

﹥>﹥吖頭↗ 提交于 2019-12-06 16:42:59
问题 I need to gather Distinct Id's from a particular table using LINQ. The catch is I also need a WHERE statement that should filter the results based only from the requirements I've set. Relatively new to having to use LINQ so much, but I'm using the following code more or less: private void WriteStuff(SqlHelper db, EmployeeHelper emp) { String checkFieldChange; AnIList tableClass = new AnIList(db, (int)emp.PersonId); var linq = tableClass.Items .Where( x => x.UserId == emp.UserId && x.Date >

Mysql statement to select distinct rows from two columns

痞子三分冷 提交于 2019-12-06 15:13:24
I'm trying to write a mysql statement that pulls only unique messages from my chat system. There is a to_id and a from_id. I want to pull one row from every conversation. The problem is if there is a row with to_id = 1 from_id = 2 and another row with to_id = 2 from_id = 1 I want both of these rows to be treated as the same. The statement I came up with is "SELECT * FROM chat_messages GROUP BY to_id,from_id ". This works except for the situation I mentioned above. Another attempt I made was using "SELECT DISTINCT least(to_id,from_id) as value1, greatest(to_id,from_id) as value2 from chat

mysql中去重 distinct 用法

亡梦爱人 提交于 2019-12-06 14:33:53
#########sample 1 mysql中去重 distinct 用法 在使用 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/shiluoliming/p

Show distinct tuples regardless of column order

纵饮孤独 提交于 2019-12-06 14:19:46
问题 Say I have following results ---------------------- | col1 | col2 | ---------------------- | a | b | | b | a | | c | d | | e | f | ---------------------- I would like to get distinct tuple regardless of column order. In other words, (a, b) and (b, a) are considered "same" because changing the order make one same as the other (a, b) == (a, b). So, after executing query should be: ---------------------- | col1 | col2 | ---------------------- | a | b | // or (b, a) | c | d | | e | f | ----------

SQL Server select distinct hell

天涯浪子 提交于 2019-12-06 12:20:44
I'm stuck on a problem with sql. I have a table with many duplicate entries with column names like:- eventnumber housenumber value1 value2 None of these column names are the primary key as there are many duplicates. What I would like to do is select into another table by distinct housenumber but I seem to get the whole table copied across, I'm using:- Select * into temp_table from schedule_temp where housenumber in (select distinct housenumerb from schedule_temp) Now I broke it down a bit and if I do:- Select distinct housenumber into temp from schedule_temp group by housenumber I get a table

性能调优4:统计信息

跟風遠走 提交于 2019-12-06 11:41:20
原文: 性能调优4:统计信息 SQL Server优化器基于开销(Cost)评估执行计划,选择开销最小的作为“最优化”的执行计划。计算开销的根据是索引及其统计信息,因此,索引和统计数据是非常重要的。查询优化器(Query Optimizer)使用统计信息对查询的开销进行评估(Estimate),选择开销最小的查询计划,作为最终的、“最优的”的执行计划。SQL Server自动为索引列或查询的数据列创建统计信息,统计信息包括三部分:头部(Header),密度向量(Density Vector) 和 分布直方图(Distribution Histogram)。 统计信息是数据分布的反馈,SQL Server根据数据更新的数量和特定的规则自动更新统计信息,一般情况下,表的数据量越大,SQL Server更新统计信息需要的数据更新量越大,随着数据的更新,有些表的数据不会及时更新,以至于统计信息过时,不能真实反映数据的分布情况,用户可以通过命令手动更新统计信息,但是更新统计信息需要扫描数据表,这可能是一个非常耗时的IO密集型操作,用户需要权衡性能的提升和资源的消耗。 一,查看统计信息 统计信息不是实时更新的,如果统计信息过期,查询优化器(Query optimizer)可能不能生成高质量的查询计划,必须有必要的调度程序,自动更新统计数据。数据库管理员(DBA)可以使用DBCC SHOW

get latest objects django

自古美人都是妖i 提交于 2019-12-06 11:21:46
I think many similar questions have already been asked, but I could not make up a copy-paste solution. So here is the problem: I periodically compute analysis on my user data for reporting. I made up a model to store these computations in my sqlite database. class report_data(models.Model): name = models.TextField() matrix = PickledObjectField() creation_date = models.DateTimeField('date of creation') I am using an append only scheme, to persist historical data, so there are many records in database, that have same name different matrix different creation date My report view now needs a set,

Getting distinct rows based on a certain field from a database in Django

痞子三分冷 提交于 2019-12-06 11:16:55
问题 I need to construct a query in Django, and I'm wondering if this is somehow possible (it may be really obvious but I'm missing it...). I have a normal query Model.objects.filter(x=True)[:5] which can return results like this: FirstName LastName Country Bob Jones UK Bill Thompson UK David Smith USA I need to only grab rows which are distinct based on the Country field, something like Model.objects.filter(x=True).distinct('Country')[:5] would be ideal but that's not possible with Django. The

C# distinct List<string> by substring

筅森魡賤 提交于 2019-12-06 10:41:23
问题 I want to remove duplicates from a list of strings. I do this by using distinct, but i want to ignore the first char when comparing. I already have a working code that deletes the duplicates, but my code also delete the first char of every string. List<string> mylist = new List<string>(); List<string> newlist = mylist.Select(e => e.Substring(1, e.Length - 1)).Distinct().ToList(); Input: "1A" ,"1B", "2A" ,"3C","4D" Output: "A","B","C","D" Right Output: "1A","2B","3C","4D" it doesn't matter if