带有视图的递归公用表表达式

时间:2015-01-30 21:03:22

标签: sql sql-server view with-statement

这就是我想要做的事情:

Create view vDetailsCommunications as
WITH Tickets AS
(
    SELECT  CallLog.CallID
          , CallLog.RecvdDate
          , Detail.ReqEffDate
          , Asgnmnt.DateAcknow
          , Asgnmnt.DateResolv
          , Asgnmnt.Assignee
          , Asgnmnt.GroupName
          , CallLog.CallType
          , Detail.ActionReq
          , Detail.action_type
          , (SELECT [Days] FROM NonWorkingDays(Asgnmnt.DateAcknow, Asgnmnt.DateResolv) AS NonWorkingDays_1) AS [Working Days]
          , DATEDIFF(day, Asgnmnt.DateAcknow, Asgnmnt.DateResolv) AS [Days]
          , (ROW_NUMBER() OVER(PARTITION BY Asgnmnt.CallID ORDER BY Asgnmnt.DateAcknow)) AS [Row Number]
    FROM    CallLog 
    INNER JOIN  Detail ON CallLog.CallID = Detail.CallID 
    INNER JOIN  Asgnmnt ON CallLog.CallID = Asgnmnt.CallID
    WHERE (CallLog.CallType = 'ID Request-PAF')
      AND (Detail.ActionReq = 'Hiring Action')
      AND (DATEDIFF (DAY, Asgnmnt.DateAcknow, Asgnmnt.DateResolv) BETWEEN 0 AND 99)
      AND (Asgnmnt.GroupName IN ('ID Admin', 'Systems Admin'))
      AND (Detail.action_type IN ('Applicant Hire', 'Re-Hire'))
)
SELECT *
FROM Tickets
WHERE [Row Number] = 1

1 个答案:

答案 0 :(得分:1)

您需要在此函数中定义架构:NonWorkingDays。

例如:

SELECT [Days] FROM dbo.NonWorkingDays(Asgnmnt.DateAcknow, Asgnmnt.DateResolv)