SQL“With As”替代方式

时间:2010-08-19 14:27:31

标签: sql row next temporary

在上一个问题中,你们帮助我从另一行获取数据。我使用的语句在MS SQL Server Managment Studio上完美运行。我可以毫无错误地运行语句,并返回我需要的数据。但是,我需要在我们的前端程序上运行此数据。当我尝试在这个程序上运行我的声明时,它就会挂起。我有一种感觉,这句话的“With As”部分引起了问题。无论如何通过将此临时表放在子查询中来重写此语句?

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

2 个答案:

答案 0 :(得分:1)

如果你只想实现它,你可以做到

;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 * INTO #Temp1 FROM Temp1


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)

以下是我最终的结果。这适用于我们使用的思科前端程序。

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