Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select distinct column

I've a query which returns the following data

enter image description here

as you can see in the image the colored groups are similar regarding column "A" i want to take the first occurrence of these rows regarding column "A" and discard the rest.

so i can end up with this result.

enter image description here

any solutions?

Thanks :)

Update:

this is the original query results enter image description here

like image 540
Kassem Avatar asked Sep 23 '12 11:09

Kassem


People also ask

How do I select one distinct column in SQL?

Adding the DISTINCT keyword to a SELECT query causes it to return only unique values for the specified column list so that duplicate rows are removed from the result set.

How can I get distinct values of all columns 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.

Can you select multiple distinct columns in SQL?

Answer. Yes, the DISTINCT clause can be applied to any valid SELECT query. It is important to note that DISTINCT will filter out all rows that are not unique in terms of all selected columns. Feel free to test this out in the editor to see what happens!

Does distinct apply to all columns in select?

Yes, DISTINCT works on all combinations of column values for all columns in the SELECT clause.


1 Answers

I would do it as follows:

WITH T(A, B, C, D, RowNum) AS 
(
    SELECT A, B, C, D, ROW_NUMBER() OVER (PARTITION BY A ORDER BY A)
    FROM MyTable
)
SELECT * FROM T
WHERE 
    RowNum = 1
like image 181
Vikdor Avatar answered Sep 23 '22 21:09

Vikdor