select语句的存储过程

时间:2014-03-01 20:33:30

标签: sql sql-server sql-server-2008 stored-procedures join

我有一个选择声明。我想在这个select语句中添加一个连接,我要添加的连接位于此代码的底部。为什么我无法为此select语句添加一个左外连接?我要添加的联接位于底部。我还需要为整个select语句编写一个存储过程:

SELECT
    FactId, UserType,
    wr.WorkRequestId, wr.XerisUserKey, 
    xu.CsuserUserID UserId, 
    u.fname UserFName, u.lname UserLName,
    b.PatientId, p.firstname PatFName, p.lastname PatLName,
    GroupId, HospiceGroupKey GroupKey, WR.ContactKey,
    C.ContactId, C.FirstName, C.LastName,
    Convert(datetime, (Convert(varchar, SD.Date, 101) + ' ' + ST.TimeOfDay ))  Start_dtm,
    Convert(datetime, (Convert(varchar, CD.Date, 101) + ' ' + CT.TimeOfDay ))  End_dtm,
    DATEDIFF(s,Convert(datetime,(Convert(varchar, SD.Date, 101) + ' ' + ST.TimeOfDay)),
    Convert(datetime, (Convert(varchar, CD.Date, 101) + ' ' + CT.TimeOfDay )))  WRDuration,
    (Convert(Decimal(18, 3), DATEDIFF(s, Convert(datetime,(Convert(varchar, SD.Date, 101) + ' ' + ST.TimeOfDay )),
    Convert(datetime, (Convert(varchar, CD.Date, 101) + ' ' + CT.TimeOfDay ))))) * 
                (Convert(Decimal(18,3),LineItemCount)/Convert(Decimal(18,3),PatientBucketItemCount)) Duration,
    CallBackNumber, WorkRequestType,
    B.LineItemCount, ArchiveLocation, Processed,
    ArchiveQueueType, TQA, Exclude, CallId
FROM 
    bi.dbo.FactWorkRequestTouches (NOlock) WR
INNER JOIN 
    bi.dbo.BridgePatientWorkRequest B ON B.WorkRequestId = WR.WorkRequestId
INNER JOIN 
    bi.dbo.dimPatient (NOlock) P ON B.PatientId = P.CphPatientID
INNER JOIN 
    bi.dbo.DimXerisUsers (NOlock) XU ON WR.XerisUserKey = XU.XerisUserKey
INNER JOIN 
    cdc.dbo.csuser (NOlock) U ON XU.CsuserUserID = u.user_id
INNER JOIN 
    bi.dbo.DimTimeOfDay (NOlock) ST ON WR.StartTimeOfDayKey = ST.TimeKey
INNER JOIN 
    bi.dbo.DimTimeOfDay (NOlock) CT ON WR.CompletedTimeOfDayKey = CT.TimeKey
INNER JOIN 
    bi.dbo.DimDate (NOlock) SD ON WR.StartDateKey = SD.DateKey
INNER JOIN 
    bi.dbo.DimDate (NOlock) CD ON WR.CompletedDateKey = CD.DateKey
LEFT OUTER JOIN 
    bi.dbo.DimContact (Nolock) C ON WR.ContactKey = C.ContactKey
WHERE 
    CompletedDateKey = '20140131'
    AND ArchiveQueueType = 0
    AND PatientBucketItemCount <> 0
    AND Exclude = 0
    AND P.ENDDate is Null

这是我想要添加到此select语句的连接

left outer join 
    ssdba.excelleRx_WebFOCUS.dbo.DimHospiceHiearchy (nolock) h on b.groupid = h.group_id