distinct

How does LINQ .distinct method sort?

。_饼干妹妹 提交于 2019-12-18 19:05:59
问题 Let's say I'm using the LINQ array .Distinct() method. The result is unordered. Well, everything is "ordered" if you know the logic used to produce the result. My question is about the result set. Will the resulting array be in the "first distinct" order or perhaps the "last distinct" order? Can I never count on any order? This is the old "remove duplicate strings" problem but I'm looking into the LINQ solution. 回答1: Assuming you mean LINQ to Objects, it basically keeps a set of all the

Select distinct values from multiple columns in same table

放肆的年华 提交于 2019-12-18 14:16:45
问题 I am trying to construct a single SQL statement that returns unique, non-null values from multiple columns all located in the same table. SELECT distinct tbl_data.code_1 FROM tbl_data WHERE tbl_data.code_1 is not null UNION SELECT tbl_data.code_2 FROM tbl_data WHERE tbl_data.code_2 is not null; For example, tbl_data is as follows: id code_1 code_2 --- -------- ---------- 1 AB BC 2 BC 3 DE EF 4 BC For the above table, the SQL query should return all unique non-null values from the two columns,

select statment performance degradation when using DISTINCT with parameters

元气小坏坏 提交于 2019-12-18 13:34:47
问题 Note for bounty - START: PARAMETERS SNIFFING (that is the only "idea" that was reported in pre-bounty questions) is not the issue here, as you can read in the "update" section at the end of the question. The problem is really related to how sql server creates execution plans for a parametrized query when distinct is used. I uploaded a very simple database backup (it works with sql server 2008 R2) here (you must wait 20 seconds before downloading). Against this DB you can try to run the

Django MySQL distinct query for getting multiple values

放肆的年华 提交于 2019-12-18 13:10:06
问题 I have a MySQL database unfortunately used with Django 1.4.1. Distinct function is only working for POSTGRESQL if i get it right. I have to make a distinct query consist of multiple values while only distinct one, Like; This one works for POSTGRE but not with MYSQL, I get the following error; DISTINCT ON fields is not supported by this database backend staff = Staff.objects.order_by('person__full_name').distinct('person__full_name') Then I tried staff = Staff.objects.values('person__full_name

SQL Server Server query - Count distinct DateTime field

青春壹個敷衍的年華 提交于 2019-12-18 12:38:19
问题 Supposing we have the following records in an SQL Server table. Date 19/5/2009 12:00:00 pm 19/5/2009 12:15:22 pm 20/5/2009 11:38:00 am What is the SQL syntax for getting something like this one? Date Count 19/5/2009 2 20/5/2009 1 回答1: You need to do any grouping on a Date only version of your datefield, such as this. SELECT CONVERT(VARCHAR(10), YourDateColumn, 101), COUNT(*) FROM YourTable GROUP BY CONVERT(VARCHAR(10), YourDateColumn, 101) I usually do this though, as it avoids conversion to

Select distinct column along with some other columns in MySQL

ぃ、小莉子 提交于 2019-12-18 11:08:28
问题 I can't seem to find a suitable solution for the following (probably an age old) problem so hoping someone can shed some light. I need to return 1 distinct column along with other non distinct columns in mySQL. I have the following table in mySQL: id name destination rating country ---------------------------------------------------- 1 James Barbados 5 WI 2 Andrew Antigua 6 WI 3 James Barbados 3 WI 4 Declan Trinidad 2 WI 5 Steve Barbados 4 WI 6 Declan Trinidad 3 WI I would like SQL statement

DISTINCT clause with WHERE

大憨熊 提交于 2019-12-18 10:41:08
问题 How can I use the DISTINCT clause with WHERE? For example: SELECT * FROM table WHERE DISTINCT email; -- email is a column name I want to select all columns from a table with distinct email addresses. 回答1: If you mean all columns whose email is unique: SELECT * FROM table WHERE email in (SELECT email FROM table GROUP BY email HAVING COUNT(email)=1); 回答2: May be by : SELECT DISTINCT email,id FROM table where id='2'; 回答3: select t1.* from YourTable as t1 inner join (select email from YourTable

How to display unique records from a has_many through relationship?

柔情痞子 提交于 2019-12-18 10:01:04
问题 I'm wondering what is the best way to display unique records from a has_many, through relationship in Rails3. I have three models: class User < ActiveRecord::Base has_many :orders has_many :products, :through => :orders end class Products < ActiveRecord::Base has_many :orders has_many :users, :through => :orders end class Order < ActiveRecord::Base belongs_to :user, :counter_cache => true belongs_to :product, :counter_cache => true end Lets say I want to list all the products a customer has

Find duplicate xelements

孤者浪人 提交于 2019-12-18 09:27:33
问题 I have the below XML <Automobiles> <Cars> <YearofMfr>2010</YearofMfr> <Mileage>12</Mileage> <MeterReading>1500</MeterReading> <Color>Red</Color> <Condition>Excellent</Condition> </Cars> <Cars> <YearofMfr>2010</YearofMfr> <Mileage>12</Mileage> <MeterReading>1500</MeterReading> <Color>Red</Color> <Condition>Excellent</Condition> </Cars> <Cars> <YearofMfr>2008</YearofMfr> <Mileage>11</Mileage> <MeterReading>20000</MeterReading> <Color>Pearl White</Color> <Condition>Good</Condition> </Cars> <

GROUP or DISTINCT after JOIN returns duplicates

守給你的承諾、 提交于 2019-12-18 09:17:10
问题 I have two tables, products and meta . They are in relation 1:N where each product row has at least one meta row via foreign key. (viz. SQLfiddle: http://sqlfiddle.com/#!15/c8f34/1) I need to join these two tables but i need to filter only unique products. When I try this query, everything is ok (4 rows returned): SELECT DISTINCT(product_id) FROM meta JOIN products ON products.id = meta.product_id but when I try to select all columns the DISTINCT rule no longer applies to results, as 8 rows