[sql] SQL How to remove duplicates within select query?

I have a table which looks like that:

alt text

As You see, there are some date duplicates, so how to select only one row for each date in that table?

the column 'id_from_other_table' is from INNER JOIN with the table above

This question is related to sql

The answer is


There are multiple rows with the same date, but the time is different. Therefore, DISTINCT start_date will not work. What you need is: cast the start_date to a DATE (so the TIME part is gone), and then do a DISTINCT:

SELECT DISTINCT CAST(start_date AS DATE) FROM table;

Depending on what database you use, the type name for DATE is different.


here is the solution for your query returning only one row for each date in that table here in the solution 'tony' will occur twice as two different start dates are there for it

SELECT * FROM 
(
    SELECT T1.*, ROW_NUMBER() OVER(PARTITION BY TRUNC(START_DATE),OWNER_NAME ORDER BY 1,2 DESC )  RNM
    FROM TABLE T1
)
WHERE RNM=1

If you want to select any random single row for particular day, then

SELECT * FROM table_name GROUP BY DAY(start_date)

If you want to select single entry for each user per day, then

SELECT * FROM table_name GROUP BY DAY(start_date),owner_name

You have to convert the "DateTime" to a "Date". Then you can easier select just one for the given date no matter the time for that date.


Select Distinct CAST(FLOOR( CAST(start_date AS FLOAT ) )AS DATETIME) from Table

Do you need any other information except the date? If not:

SELECT DISTINCT start_date FROM table;