views:

44

answers:

2

I have data like this. Following is the sample data.

alt text

I want to get all those CustomerIDs which has date diff of 2 hours in DateCreated of Enquiry.

For example CustomerId 10602 has 3 enquiries . If the time difference is 2 hours in any of these three enquiries then this CustomerId should be in my result set. Same for the other Customers.

Thanks

+7  A: 
SELECT [CustomerId]
  FROM [Table]
 WHERE [DateCreated] >= DATEADD(hour, -2, GETDATE())
   AND [CustomerId] = xxx;
Ardman
Thanks for your answer. The difference should be among enquiries of same Customer not with current date.
Muhammad Kashif Nadeem
Ah, you want Thomas's answer then. :o)
Ardman
@Ardman thanks for the answer. Your answer is correct, my question was ambiguous.
Muhammad Kashif Nadeem
A: 

This presumes that EnquiryId is the PK of the table. I also presumed you want to count items within two hours of each other.

Select Distinct T1.CustomerId
From Table As T1
Where Exists    (
                Select 1
                From Table As T2
                Where T2.CustomerId = T1.CustomerId
                    And T2.EnquiryId <> T1.EnquiryId
                    And Abs(DateDiff(hh, T1.DateCreated, T2.DateCreated)) <= 2
               )
Thomas
@Ardman - Yeah. Fixed that in my post rather than via a comment.
Thomas
Thanks @Thomas for the answer.
Muhammad Kashif Nadeem