Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select records which are repeated twice!

Tags:

I have a table in SQL Server with these records :

ID         Date     Time
--     ---------    ----
1      09/05/02     6:00
2      09/05/02     8:00
3      09/05/03     6:00
4      09/05/04     8:00
5      09/05/04     6:00

I would like to select those ID who have records in a day which are repeated twice or multiple of two.

How would I do this in SQL Server?

like image 788
LIX Avatar asked Feb 10 '10 07:02

LIX


2 Answers

Something like:

select table_1.id, table_1.date from
table as table_1 inner join table as table_2 on 
table_1.date = table_2.date 
and 
table_1.id <> table_2.id

should work alright.

like image 169
Coxy Avatar answered Nov 13 '22 14:11

Coxy


this query just select records with ID=1 and days which are repeated twice or multiple of 2 :

SELECT  *
FROM     MyTable
WHERE  (Date IN
          (SELECT  Date
           FROM  MyTable
           WHERE  (ID = 1)
           GROUP BY Date
          HAVING  (COUNT(Date) % 2 = 0)
          )
       )
 AND (ID = 1)
like image 24
LIX Avatar answered Nov 13 '22 15:11

LIX