【问题标题】:SQL "With As" Alternative WaySQL“With As”替代方式
【发布时间】:2011-04-01 04:02:20
【问题描述】:

在上一个问题中,你们帮助我从不同的行中获取数据。我使用的语句在 MS SQL Server Management 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

【问题讨论】:

  • 程序中的sql如何执行?
  • 我将在 MS SQL Studio 2005 中正确执行的代码复制并粘贴到我们的 Cisco 前端程序中。我收到无效查询的错误代码。关键字 WHERE 附近的错误。这很奇怪,因为我在语句中没有 WHERE 子句。我尝试在语句中添加一个空白和一个有效的 where 子句,但这只会导致程序挂起。没有给出错误代码。
  • 这听起来像是你的前端程序修改东西的问题?如果您将所有查询放到一个视图中,然后尝试在前端程序中从中选择呢?
  • 不说了。我对数据库的任何部分都没有写入权限。

标签: sql row next temporary


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    如果你只是想实现它,你可以这样做

    ;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
    

    虽然在前端程序上运行时挂起,但它并不真正理解您的意思。您是完全按照书面形式使用查询,还是以某种方式对其进行参数化?

    您是否两次都针对相同的数据运行它?

    【讨论】:

    • 我在这两种情况下都针对同一个数据库运行此查询。前端程序是 Cisco 程序。使用你的方式最终会用所有这些临时表填充服务器还是在使用后被删除?
    • @Gilbert - 连接结束时服务器不会自动删除临时表。实际上,您可能会更好地明确删除,但无论如何,在您澄清之后,我怀疑这种使用临时表的更改是否会产生任何影响。
    • 我仍然收到无效查询的错误代码。关键字 WHERE 附近出错。
    • @Gilbert - 是的。听起来您的前端应用程序必须在做一些奇怪的事情。我建议使用 Profiler 查看发送的语句,但我认为您没有权限这样做。
    猜你喜欢
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2021-01-27
    • 1970-01-01
    • 2021-07-10
    相关资源
    最近更新 更多