Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:56:47+00:00 2026-06-04T17:56:47+00:00

The query I am using is: SELECT a.What_Tech, a.callid FirstCallid, b.Callid SecondCallid, a.[segStart_dateTime] FirstCallTime,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:56:50+00:00Added an answer on June 4, 2026 at 5:56 pm

    CROSS APPLY will enable the join to take the first row only from b7 thus 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.

    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 testdata a
     cross apply
     (
       select top 1
           CallId,
           [segStart_dateTime]
         from testdata
      -- Filtering by a.calling_pty is missing 
        where testdata.segStart_dateTime > dateadd (second, 60, a.segStart_dateTime)
          and testdata.segStart_dateTime <= dateadd (second, 1800, a.segStart_dateTime)
        order by testdata.segStart_dateTime
    ) b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using this SQL query: Select * from table_name what happens if the name
I am creating a file from a SELECT query using sqlplus with SPOOL command.
How to write the query using linq? SELECT (SELECT SUM([quantity] * [price_usd]) FROM [vi].[dbo].[SALES]
I am using this query: SELECT DISTINCT pat.PublicationID FROM dbo.PubAdvTransData AS pat INNER JOIN
I am using a SELECT query to get the data from a number of
Is there is any way to get the select Random Records using query from
I am using a query inside PHP as: $query = 'SELECT * from #__chronoforms_UploadAuthor
Query I'm using: SELECT COUNT(*), SUM(amount) AS amount, FROM_UNIXTIME(added, '%W (%e/%m)') AS daily FROM
I try to execute the following query using java.sql.PreparedStatement: SELECT NVL (tb.ddate, '2002-10-15') FROM
i wanted to get the data from my second query using the first query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.