Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select rows based on distinct values of A COLUMN only

I need to query a table in order to return rows, but I am not able to query the table correctly. Here is my table view:

Id                MailId          EmailAddress          Name 1                 1               [email protected]               Mr. A 2                 1               [email protected]               Mr. B 3                 1               [email protected]               Mr. C 4                 1               [email protected]               Mr. D 5                 1               [email protected]               Mr. A 6                 2               [email protected]               Mr. E 7                 2               [email protected]               Mr. A 8                 3               [email protected]               Mr. F 9                 4               [email protected]               Mr. D   10                5               [email protected]               Mr. F 11                6               [email protected]               Mr. D 

The result set should return:

Id                MailId          EmailAddress          Name 1                 1               [email protected]               Mr. A 2                 1               [email protected]               Mr. B 3                 1               [email protected]               Mr. C 4                 1               [email protected]               Mr. D 6                 2               [email protected]               Mr. E 8                 3               [email protected]               Mr. F 

In other words: first, I want to select distinct e-mail addresses, and then return rows containing distinct e-mail addresses.

Note: Just using the "Distinct" keyword will not work here, as it will select distinct rows. My requirement is to select distinct email addresses, and then to select rows containing those addresses.

Edit: I cannot use the "Group By" keyword either, because for this I will also have to Group By with Id (which is the PK) and doing this will return two rows with the same EmailAddress values but with different Ids.

like image 313
user576510 Avatar asked Sep 17 '11 11:09

user576510


People also ask

How do you SELECT unique records from the particular column?

The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

How do I SELECT distinct rows in pandas?

And you can use the following syntax to select unique rows across specific columns in a pandas DataFrame: df = df. drop_duplicates(subset=['col1', 'col2', ...])

How can I get distinct values for a particular column in SQL?

To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.


2 Answers

Looking at your output maybe the following query can work, give it a try:

SELECT * FROM tablename WHERE id IN (SELECT MIN(id) FROM tablename GROUP BY EmailAddress) 

This will select only one row for each distinct email address, the row with the minimum id which is what your result seems to portray

like image 62
danishgoel Avatar answered Sep 23 '22 00:09

danishgoel


Try this - you need a CTE (Common Table Expression) that partitions (groups) your data by distinct e-mail address, and sorts each group by ID - smallest first. Then you just select the first entry for each group - that should give you what you're looking for:

;WITH DistinctMails AS (     SELECT ID, MailID, EMailAddress, NAME,         ROW_NUMBER() OVER(PARTITION BY EMailAddress ORDER BY ID) AS 'RowNum'     FROM dbo.YourMailTable ) SELECT * FROM DistinctMails WHERE RowNum = 1 

This works on SQL Server 2005 and newer (you didn't mention what version you're using...)

like image 32
marc_s Avatar answered Sep 24 '22 00:09

marc_s