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 6155497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:26:44+00:00 2026-05-23T20:26:44+00:00

In a previous question, you guys helped me grab data from a different row.

  • 0

In a previous question, you guys helped me grab data from a different row. The statement I am using works perfectly on the MS SQL Server Managment Studio. I can run the statement without any errors and I return the data I need. However, I need to run this data on our frontend program. When I try to run my statement on this program, it just hangs. I have a feeling that the “With As” part of this statement is causing problems. Is there anyway to rewrite this statement by putting this temporary table in a subquery?

WITH Temp1 AS (SELECT
SkillTargetID = Agent_Logout.SkillTargetID,
LogoutDateTime = Agent_Logout.LogoutDateTime,
LogonDate = DateAdd(s,-1 * Agent_Logout.LoginDuration,Agent_Logout.LogoutDateTime),
ROW_NUMBER() OVER(PARTITION BY Agent_Logout.SkillTargetID ORDER BY Agent_Logout.LogoutDateTime ASC) RowVersion,
LoginDuration = Agent_Logout.LoginDuration
FROM Agent_Logout)

SELECT
AgentID = Base.SkillTargetID,
LogonDate = Base.LogonDate,
BaseLogout = Base.LogoutDateTime,
BaseDuration = Base.LoginDuration,
NextLogon = Temp1.LogonDate,
LogoutDuration = DateDiff(s,Base.LogoutDateTime,Temp1.LogonDate)
FROM Temp1 Base
LEFT JOIN Temp1 ON Base.SkillTargetID = Temp1.SkillTargetID
AND Base.RowVersion = Temp1.RowVersion-1
  • 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-05-23T20:26:44+00:00Added an answer on May 23, 2026 at 8:26 pm

    Below is what I ended up with. This works with the Cisco front-end program we use.

    DECLARE @dtStartDateTime DATETIME, @dtEndDateTime DATETIME, @agentid VARCHAR
    SET @dtStartDateTime = :start_date SET @dtEndDateTime = :end_date
    SELECT
        FullName = Temp1.FullName,
        AgentID = Temp1.SkillTargetID,
        LogonDate = Temp1.LogonDate,
        LogoutDate = Temp1.LogoutDateTime,
        LoginDuration = Temp1.LoginDuration,
        RowVersion# = Temp1.RowVersion,
        AgentID2 = Temp2.SkillTargetID,
        LogonDate2 = Temp2.LogonDate,
        LogoutDate2 = Temp2.LogoutDateTime,
        RowVersion#2 = Temp2.RowVersion,
        LogoutDuration = DateDiff(s,Temp1.LogoutDateTime,Temp2.LogonDate)
    
    FROM
    
    (SELECT
        FullName = Person.LastName + ', ' + Person.FirstName,
        SkillTargetID = Agent_Logout.SkillTargetID,
        LogoutDateTime = Agent_Logout.LogoutDateTime,
        LogonDate = DateAdd(s,-1 * Agent_Logout.LoginDuration,Agent_Logout.LogoutDateTime),
        ROW_NUMBER() OVER(PARTITION BY Agent_Logout.SkillTargetID ORDER BY Agent_Logout.LogoutDateTime ASC) as RowVersion,
        LoginDuration = Agent_Logout.LoginDuration
        FROM Agent_Logout, Agent, Person
        WHERE Agent_Logout.SkillTargetID = Agent.SkillTargetID and Agent.PersonID = Person.PersonID
    ) Temp1,
    
    (SELECT
        SkillTargetID = Agent_Logout.SkillTargetID,
        LogoutDateTime = Agent_Logout.LogoutDateTime,
        LogonDate = DateAdd(s,-1 * Agent_Logout.LoginDuration,Agent_Logout.LogoutDateTime),
        ROW_NUMBER() OVER(PARTITION BY Agent_Logout.SkillTargetID ORDER BY Agent_Logout.LogoutDateTime ASC) as RowVersion
        FROM Agent_Logout
        WHERE Agent_Logout.SkillTargetID = Agent_Logout.SkillTargetID
    ) Temp2
    
    WHERE Temp1.SkillTargetID = Temp2.SkillTargetID and Temp1.RowVersion = (Temp2.RowVersion - 1) AND
    (Temp1.LogonDate >= :start_date 
    And Temp1.LogonDate <= :end_date) AND
    Temp1.SkillTargetID IN (:agentid)
    ORDER BY Temp1.SkillTargetID, Temp1.RowVersion
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok guys working on search suggestions using jQuery-UI AutoComplete with results from sql-sever 2008
Thanks to the guys that helped me with my previous question (linked just for
In previous question of mine, someone had meantioned that using Semaphores were expensive in
This question follows on from a previous question, that has raised a further issue.
Continuing from my previous question , is there a comprehensive document that lists all
Right guys, seeing as you were so helpful last time with my previous question
Hey guys this is a followup to my previous question . I now have
Continuing my previous question Why I cannot derive from long? I found an interesting
In a similar vein to my previous question I again ask the SO guys
This is a continuation question from a previous question I have asked I now

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.