The query I am using is:
SELECT
a.What_Tech,
a.callid FirstCallid,
b.Callid SecondCallid,
a.[segStart_dateTime] FirstCallTime,
b.[segStart_dateTime] SecondCallTime,
DateDiff(second, a.[segStart_dateTime], b.[segStart_dateTime]) as [myDiff]
FROM CallVolume.AreaGeneratedActivity a
JOIN CallVolume.AreaGeneratedActivity b on
a.calling_pty = b.calling_pty
and a.[segStart_dateTime] < b.[segStart_dateTime]
and dbo.getdateonly(a.segStart_dateTime) = dbo.getdateonly(b.segStart_dateTime)
WHERE ABS(DateDiff(second, a.[segStart_dateTime], b.[segStart_dateTime])) > 60
and ABS(DateDiff(second, a.[segStart_dateTime], b.[segStart_dateTime])) <= 1800
and what is being returned is:
|What_Tech|FirstCallid|SecondCallid|FirstCallTime|SecondCallTime|myDiff | Tech1 | 21134601 | 21136023 | 09:19:41 | 09:20:43 | 62 | Tech1 | 21134601 | 21134507 | 09:19:41 | 09:19:41 | 886 | Tech1 | 21134601 | 21134602 | 09:19:41 | 09:34:27 | 887
What I want is different FirstCallids:
|What_Tech|FirstCallid|SecondCallid|FirstCallTime|SecondCallTime|myDiff | Tech1 | 21134601 | 21134676 | 09:19:41 | 09:20:43 | 62 | Tech1 | 21136023 | 21136024 | 09:34:27 | 09:35:27 | 1
Here is some sample data from the table:
INSERT [dbo].[TestData](callid, What_Tech, segStart_dateTime, segStop_dateTime, duration)
SELECT 21134601, N'Tech1', '20120307 09:19:41', '20120307 09:20:35', 54 UNION ALL
SELECT 21134676, N'Tech1', '20120307 09:20:43', '20120307 09:21:30', 47 UNION ALL
SELECT 21136023, N'Tech1', '20120307 09:34:27', '20120307 09:36:54', 147 UNION ALL
SELECT 21148838, N'Tech1', '20120307 12:00:29', '20120307 12:02:05', 96 UNION ALL
SELECT 21149159, N'Tech1', '20120307 12:04:32', '20120307 12:05:46', 74 UNION ALL
SELECT 21163170, N'Tech1', '20120307 14:50:08', '20120307 14:52:36', 148
Please note that the sample data from what is being returned and what I want are just examples and not directly from the sample data given in the INSERT statements
The query posted (once I remove the join condition for calling_party which is missing from the INSERT and change the function call to an inline calculation) produces these results given the provided sample data:
What_Tech FirstCallId SecondCallId FirstCallTime SecondCallTime myDiff
--------- ----------- ------------ ------------------- ------------------- ------
Tech1 21134601 21134676 2012-03-07 09:19:41 2012-03-07 09:20:43 62
Tech1 21134601 21136023 2012-03-07 09:19:41 2012-03-07 09:34:27 886
Tech1 21134676 21136023 2012-03-07 09:20:43 2012-03-07 09:34:27 824
Tech1 21148838 21149159 2012-03-07 12:00:29 2012-03-07 12:04:32 243
Given that, can you please define what results you expect, given the actual sample data you’ve provided, instead of showing desired results from a slightly different set of data that we can’t see?
CROSS APPLY will enable the join to take the first row only from
b7thus eliminating duplication problems you were facing. BUT! This will only produce satisfying results if you allow calling_pty’s third call to be represented as pair (second_call, third_call), i.e. SecondCall can be FirstCall in some other row. If you want to eliminate all calls after second, you will need different approach.Here is Sql Fiddle DEMO version.