Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite select all records for which count is greater than 1

Tags:

sqlite

This is my schema:

create table events(
 event_type integer not null,
 value integer not null,
 time timestamp not null,
 unique (event_type ,time)
);

insert into events values
(2,   5,  '2015-05-09 12:42:00'),
(4, -42,  '2015-05-09 13:19:57'),
(2,   2,  '2015-05-09 14:48:39'),
(2,   7,  '2015-05-09 13:54:39'),
(3,  16,  '2015-05-09 13:19:57'),
(3,  20,  '2015-05-09 15:01:09')   

I would like to display all the records for which event_type has been registered more than once. As you can see in the schema, event_type 2 and 3 is occurred more than once. Following is the query I have used which selecting only one record for event_type 2 and 3:

select event_type, value, time from events
group by event_type
having count(event_type) > 1;

I would like to see the query that would display all the records with event_type 2 and 3. Thanks in advance for all the help.

like image 942
Santosh M. Avatar asked Jan 07 '23 06:01

Santosh M.


1 Answers

select e.event_type, e.value, e.time
  from events e
  join ( select event_type 
           from events
          group by event_type
         having count(*) > 1 ) b
    on e.event_type = b.event_type;

For me, this returns:

2|5|2015-05-09 12:42:00
2|7|2015-05-09 13:54:39
2|2|2015-05-09 14:48:39
3|16|2015-05-09 13:19:57
3|20|2015-05-09 15:01:09

Reference: Show all duplicated rows

like image 69
Christian Henry Avatar answered Mar 24 '23 22:03

Christian Henry