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
1452
votes
23 answers

LINQ's Distinct() on a particular property

I am playing with LINQ to learn about it, but I can't figure out how to use Distinct when I do not have a simple list (a simple list of integers is pretty easy to do, this is not the question). What I if want to use Distinct on a List on…
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
908
votes
22 answers

How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?

I have a table of player performance: CREATE TABLE TopTen ( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, home INT UNSIGNED NOT NULL, `datetime`DATETIME NOT NULL, player VARCHAR(6) NOT NULL, resource INT NOT NULL ); What query will return…
Kaptah
  • 9,785
  • 4
  • 22
  • 19
554
votes
5 answers

How do I (or can I) SELECT DISTINCT on multiple columns?

I need to retrieve all rows from a table where 2 columns combined are all different. So I want all the sales that do not have any other sales that happened on the same day for the same price. The sales that are unique based on day and price will get…
sheats
  • 33,062
  • 15
  • 45
  • 44
476
votes
19 answers

MySQL: Select DISTINCT / UNIQUE, but return all columns?

SELECT DISTINCT field1, field2, field3, ...... FROM table; I am trying to accomplish the following SQL statement, but I want it to return all columns. Is this possible? Something like this: SELECT DISTINCT field1, * FROM table;
aryaxt
  • 76,198
  • 92
  • 293
  • 442
458
votes
26 answers

Is there any difference between GROUP BY and DISTINCT

I learned something simple about SQL the other day: SELECT c FROM myTbl GROUP BY C Has the same result as: SELECT DISTINCT C FROM myTbl What I am curious of, is there anything different in the way an SQL engine processes the command, or are they…
Brettski
  • 19,351
  • 15
  • 74
  • 97
431
votes
14 answers

SQL to find the number of distinct values in a column

I can select all the distinct values in a column in the following ways: SELECT DISTINCT column_name FROM table_name; SELECT column_name FROM table_name GROUP BY column_name; But how do I get the row count from that query? Is a subquery required?
Christian Oudard
  • 48,140
  • 25
  • 66
  • 69
401
votes
11 answers

Pandas 'count(distinct)' equivalent

I am using Pandas as a database substitute as I have multiple databases (Oracle, SQL Server, etc.), and I am unable to make a sequence of commands to a SQL equivalent. I have a table loaded in a DataFrame with some columns: YEARMONTH, CLIENTCODE,…
Adriano Almeida
  • 5,186
  • 5
  • 20
  • 28
319
votes
16 answers

What's faster, SELECT DISTINCT or GROUP BY in MySQL?

If I have a table CREATE TABLE users ( id int(10) unsigned NOT NULL auto_increment, name varchar(255) NOT NULL, profession varchar(255) NOT NULL, employer varchar(255) NOT NULL, PRIMARY KEY (id) ) and I want to get all unique values of…
vava
  • 24,851
  • 11
  • 64
  • 79
315
votes
8 answers

Select unique or distinct values from a list in UNIX shell script

I have a ksh script that returns a long list of values, newline separated, and I want to see only the unique/distinct values. It is possible to do this? For example, say my output is file suffixes in a…
brabster
  • 42,504
  • 27
  • 146
  • 186
284
votes
4 answers

Count the occurrences of DISTINCT values

I am trying to find a MySQL query that will find DISTINCT values in a particular field, count the number of occurrences of that value and then order the results by the count. example db id name ----- ------ 1 Mark 2 …
JimmyJ
  • 4,311
  • 3
  • 27
  • 25
247
votes
6 answers

Get a list of distinct values in List

In C#, say I have a class called Note with three string member variables. public class Note { public string Title; public string Author; public string Text; } And I have a list of type Note: List Notes = new List(); What…
Darrel Hoffman
  • 4,436
  • 6
  • 29
  • 41
246
votes
5 answers

postgresql COUNT(DISTINCT ...) very slow

I have a very simple SQL query: SELECT COUNT(DISTINCT x) FROM table; My table has about 1.5 million rows. This query is running pretty slowly; it takes about 7.5s, compared to SELECT COUNT(x) FROM table; which takes about 435ms. Is there any way…
ferson2020
  • 3,015
  • 3
  • 18
  • 26
194
votes
9 answers

Distinct by property of class with LINQ

I have a collection: List cars = new List(); Cars are uniquely identified by their property CarCode. I have three cars in the collection, and two with identical CarCodes. How can I use LINQ to convert this collection to Cars with unique…
user278618
  • 19,306
  • 42
  • 126
  • 196
193
votes
4 answers

Postgres: Distinct but only for one column

I have a table on pgsql with names (having more than 1 mio. rows), but I have also many duplicates. I select 3 fields: id, name, metadata. I want to select them randomly with ORDER BY RANDOM() and LIMIT 1000, so I do this is many steps to save some…
NovumCoder
  • 4,349
  • 9
  • 43
  • 58
189
votes
18 answers

How to select distinct rows in a datatable and store into an array

I have a dataset objds. objds contains a table named Table1. Table1 contains column named ProcessName. This ProcessName contains repeated names.So i want to select only distinct names.Is this possible. intUniqId[i] =…
Ahmed Atia
  • 17,848
  • 25
  • 91
  • 133
1
2 3
99 100