-1

I am looking for an SQL query to give me a list of duplicate entries in a table. However, there are 3 different columns to take into account. First is an ID, Second is a Name, and third is a Date. The situation is that there are multiple Names that are assigned with the same ID, and there are multiple records of those in a day, which makes THOUSANDS of different records per day.

I already filtered it so that only results for the past 7 days will show, but the amount of records is still too much for me to extract. I just want to decrease the number of rows in the output order to properly extract the results.

Sample

|--id-|--name--|-------date------|
|  1  |   a    |5-9-2015, 10:00am|
|  1  |   a    |5-8-2015, 10:02am|
|  1  |   a    |5-8-2015, 11:00am|
|  1  |   b    |5-8-2015, 10:00am|
|  1  |   b    |5-8-2015, 10:02am|
|  1  |   c    |5-8-2015, 10:00am|
|  2  |   d    |5-8-2015, 10:00am|

expected output

|--id-|--name--|
|  1  |   a    |
|  1  |   b    |
|  1  |   c    |
|  2  |   d    |

Inclusion of entries without any duplicates are fine. The important thing is to only return a single record of a unique id-name combination for a day.

Thanks in advance for any help that you can give.

Programmer
  • 291
  • 1
  • 3
  • 14
  • Are you using MySQL or Oracle? Please tag your question with the database you are actually using. – Gordon Linoff May 18 '15 at 11:20
  • 3
    *The important thing is to only return a single record of a unique id-name combination for a day* But your expected output is not considering the date. Else, you will have two rows for 1 as id and 'a' as name. – Lalit Kumar B May 18 '15 at 11:22
  • use `Group by` command in mysql or sql query – Asif May 18 '15 at 11:34

3 Answers3

3

You can get the combinations as:

select distinct id, name
from sample;

If you want duplicates, using group by and having:

select id, name
from sample
group by id, name
having count(*) > 1;

EDIT:

If you want this by date, then add date(date) to the group by and perhaps select clauses.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 2
    Per OP, *The important thing is to only return a single record of a unique id-name combination for a day*. Your query doesn't consider the date. I see that the expected output is not according to the question description. – Lalit Kumar B May 18 '15 at 11:25
0

To return single id-name data per day you can use this:

select id, name
from tab
group by id, name, date(date)
Robert
  • 25,425
  • 8
  • 67
  • 81
0

The DATE() function extracts the date part of a date or date/time expression.

select id,name 
from sample 
group by id,name,DATE(date) 
having count(*)>1;
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61