SQL How to remove duplicates within select query?

前端 未结 7 761
甜味超标
甜味超标 2021-01-04 07:05

I have a table which looks like that:

\"alt

As You see, there are some date duplicates, so how t

相关标签:
7条回答
  • 2021-01-04 07:08

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

    SELECT DISTINCT start_date FROM table;
    
    0 讨论(0)
  • 2021-01-04 07:09

    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
    
    0 讨论(0)
  • 2021-01-04 07:14

    You mention that there are date duplicates, but it appears they're quite unique down to the precision of seconds.

    Can you clarify what precision of date you start considering dates duplicate - day, hour, minute?

    In any case, you'll probably want to floor your datetime field. You didn't indicate which field is preferred when removing duplicates, so this query will prefer the last name in alphabetical order.

     SELECT MAX(owner_name), 
            --floored to the second
            dateadd(second,datediff(second,'2000-01-01',start_date),'2000-01-01') AS StartDate
     From   MyTable
     GROUP BY dateadd(second,datediff(second,'2000-01-01',start_date),'2000-01-01')
    
    0 讨论(0)
  • 2021-01-04 07:19
    Select Distinct CAST(FLOOR( CAST(start_date AS FLOAT ) )AS DATETIME) from Table
    
    0 讨论(0)
  • 2021-01-04 07:25

    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.

    0 讨论(0)
  • 2021-01-04 07:32

    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
    
    0 讨论(0)
提交回复
热议问题