0

前の質問で、皆さんは私が別の行からデータを取得するのを手伝ってくれました。私が使用しているステートメントは、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
4

2 に答える 2

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

ただし、フロントエンドプログラムで実行しているときにハングするという意味がよくわかりません。クエリを記述どおりに使用していますか、または何らかの方法でパラメータ化していますか?

両方とも同じデータに対して実行していますか?

于 2010-08-19T14:31:22.477 に答える
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
于 2011-07-15T12:42:05.843 に答える