Questions tagged [distinct]

The DISTINCT keyword is used to remove duplicate values from a result of a SQL or SPARQL query.

The DISTINCT keyword is used to remove duplicate values from a result of a SQL or SPARQL query.

The SQL 92 Standard defines DISTINCT as:

Two values are said to be not distinct if either: both are the null value, or they compare equal... Otherwise they are distinct. Two rows (or partial rows) are distinct if at least one of their pairs of respective values is distinct. Otherwise they are not distinct.

The SPARQL 1.1 Specification describes DISTINCT as:

The DISTINCT solution modifier eliminates duplicate solutions. Only one solution that binds the same variables to the same RDF terms is returned from the query.

5114 questions
186
votes
8 answers

How to "select distinct" across multiple data frame columns in pandas?

I'm looking for a way to do the equivalent to the SQL SELECT DISTINCT col1, col2 FROM dataframe_table The pandas sql comparison doesn't have anything about distinct. .unique() only works for a single column, so I suppose I could concat the…
Jody
  • 8,021
  • 4
  • 26
  • 29
175
votes
9 answers

Entity Framework select distinct name

How can I do this SQL query with Entity Framework? SELECT DISTINCT NAME FROM TestAddresses
patrick
  • 16,091
  • 29
  • 100
  • 164
161
votes
8 answers

LINQ Select Distinct with Anonymous Types

So I have a collection of objects. The exact type isn't important. From it I want to extract all the unique pairs of a pair of particular properties, thusly: myObjectCollection.Select(item=>new { …
GWLlosa
  • 23,995
  • 17
  • 79
  • 116
159
votes
13 answers

How to use DISTINCT and ORDER BY in same SELECT statement?

After executing the following statement: SELECT Category FROM MonitoringJob ORDER BY CreationDate DESC I am getting the following values from the database: test3 test3 bildung test4 test3 test2 test1 but I want the duplicates removed, like…
r.r
  • 7,023
  • 28
  • 87
  • 129
149
votes
8 answers

LINQ: Distinct values

I have the following item set from an XML: id category 5 1 5 3 5 4 5 3 5 3 I need a distinct list of these items: 5 1 5 3 5 4 How can I distinct for…
balint
  • 3,391
  • 7
  • 41
  • 50
134
votes
4 answers

Select DISTINCT individual columns in django?

I'm curious if there's any way to do a query in Django that's not a "SELECT * FROM..." underneath. I'm trying to do a "SELECT DISTINCT columnName FROM ..." instead. Specifically I have a model that looks like: class ProductOrder(models.Model): …
jamida
  • 2,869
  • 5
  • 25
  • 22
133
votes
14 answers

Laravel Eloquent - distinct() and count() not working properly together

So I'm trying to get the number of distinct pids on a query, but the returned value is wrong. This is what I try to do: $ad->getcodes()->groupby('pid')->distinct()->count() what returns the value "2", while the value it should return, should be…
Inigo EC
  • 2,178
  • 3
  • 22
  • 31
121
votes
5 answers

How to display unique records from a has_many through relationship?

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 <…
Andy Harvey
  • 12,333
  • 17
  • 93
  • 185
120
votes
4 answers

LINQ Distinct operator, ignore case?

Given the following simple example: List list = new List() { "One", "Two", "Three", "three", "Four", "Five" }; CaseInsensitiveComparer ignoreCaseComparer = new CaseInsensitiveComparer(); var distinctList =…
Ash
  • 60,973
  • 31
  • 151
  • 169
108
votes
11 answers

How do you create a Distinct query in HQL

Is there a way to create a Distinct query in HQL. Either by using the "distinct" keyword or some other method. I am not sure if distinct is a valid keywork for HQL, but I am looking for the HQL equivalent of the SQL keyword "distinct".
Mike Pone
  • 18,705
  • 13
  • 53
  • 68
104
votes
10 answers

MySQL SELECT DISTINCT multiple columns

Let's say I have columns a, b c, d in a table in a MySQL database. What I'm trying to do is to select the distinct values of ALL of these 4 columns in my table (only the distinct values). I tried stuff like: SELECT DISTINCT a,b,c,d FROM…
user765368
  • 19,590
  • 27
  • 96
  • 167
98
votes
10 answers

Group by minimum value in one field while selecting distinct rows

Here's what I'm trying to do. Let's say I have this table t: key_id | id | record_date | other_cols 1 | 18 | 2011-04-03 | x 2 | 18 | 2012-05-19 | y 3 | 18 | 2012-08-09 | z 4 | 19 | 2009-06-01 | a 5 | 19 | 2011-04-03 |…
user2765924
  • 983
  • 1
  • 6
  • 4
97
votes
8 answers

count distinct values in spreadsheet

I have a Google spreadsheet with a column that looks like this: City ---- London Paris London Berlin Rome Paris I want to count the appearances of each distinct city (so I need the city name and the number of appearances). City |…
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
97
votes
10 answers

How to use LINQ Distinct() with multiple fields

I have the following EF class derived from a database (simplified) class Product { public string ProductId; public string ProductName; public string CategoryId; public string CategoryName; } ProductId is the Primary Key of the…
CiccioMiami
  • 8,028
  • 32
  • 90
  • 151
91
votes
1 answer

Huge performance difference when using GROUP BY vs DISTINCT

I am performing some tests on a HSQLDB server with a table containing 500 000 entries. The table has no indices. There are 5000 distinct business keys. I need a list of them. Naturally I started with a DISTINCT query: SELECT DISTINCT…
Martin Dimitrov
  • 4,796
  • 5
  • 46
  • 62