引入子查询时,只能在选择列表中指定一个表达式

时间:2015-01-26 14:20:12

标签: sql stored-procedures sql-server-2012 exists

我有这个存储过程...

 SELECT
     LTRIM(RTRIM([OF].OF_DISPLAYNAME)) AS [Officer_Name], 
     LTRIM(RTRIM([OF].OF_PIN)) AS Officer_Pin,
     LTRIM(RTRIM([TO].TO_ACTIVITY)) AS Template_Site,
     AC.AC_NAME AS Site_Name,      
     ZN.ZN_MANAGER AS Site_Manager,
     AR.AR_DESCRIPTION AS Scheduler,
     CONVERT(VARCHAR(10), @WeekCommencing, 6) AS [Week_Commencing],
     CONVERT(VARCHAR(10), [OF].OF_DOL, 6) AS Leave_Date,
     CAST((SELECT 
                (ISNULL(SUM(RO.RO_SHIFT_LENGTH - RO_BREAK_LENGTH), 0.0)/60.0)
           FROM 
                RoleCall.dbo.ROSTER AS RO WITH(NOLOCK) 
           WHERE
                RO.RO_OFFICER = [OF].OF_PIN
                AND RO.RO_SHIFT_START BETWEEN @WeekCommencing AND @WeekEnding
                AND RO.RO_STATUS IN ('COMP', 'CONF', 'TODO')
     ) AS DECIMAL(11,2)) AS HoursSum, 
     RSWH.dbo.RSWHF_GetMarker([OF].OF_PIN, @WeekCommencing, @WeekEnding) AS Marker, 
     CONVERT(VARCHAR(10), (SELECT MAX(RO.RO_SHIFT_START)
                           FROM RoleCall.dbo.ROSTER AS RO WITH(NOLOCK) 
                           WHERE RO.RO_OFFICER = [OF].OF_PIN
                             AND RO.RO_SHIFT_START < GETDATE()
                             AND RO.RO_STATUS IN ('COMP', 'CONF', 'TODO')
      ), 6) AS Last_Worked_Shift,
      CONVERT(VARCHAR(10), (SELECT TOP 1
                                HC.Effective_Date
                            FROM HR.dbo.HRST_Contract AS HC WITH(NOLOCK) 
                            WHERE 
        HC.Of_Pin = [OF].OF_PIN
        AND HC.SD_Contract = 1
    ORDER BY
        Effective_Date DESC
), 6) AS Contract_Sent_Date,
CONVERT(VARCHAR(10),
(
    SELECT TOP 1
        RO.RO_SHIFT_START
    FROM 
        Rolecall.dbo.Roster AS RO WITH(NOLOCK)
    WHERE 
        RO.RO_Activity = 'ENDASSIG'
        AND RO.RO_STATUS <> 'CANC'
        AND RO.RO_OFFICER =[OF].OF_PIN
        AND RO.RO_SHIFT_START < @WeekEnding
    ORDER BY
        RO.RO_SHIFT_START DESC
), 6) AS Ass_End_Date,
ISNULL(CreatedBy, '') AS Checked_By,

---ISSUE IS OCCURRING HERE---- 
    (
    SELECT 
        SUM(a.[Value*NumofActivity]) + max(b.NumOfHoursWorkedInTheLast12Weeks) AS [TotalHoursWorkedInTheLast12Weeks],Markers
    FROM
        (
            SELECT 
                    RO_OFFICER [RO_OFFICER],RO_ACTIVITY , COUNT(RO_ACTIVITY)*Value as [Value*NumofActivity]
            FROM    
                    RoleCall.dbo.ROSTER as ro inner join [RSWH].DBO.[RSWHT_Hours_Assigned_To_Markers_LookUp] AS MR on RO_ACTIVITY = MR.MARKER
            WHERE 
                    ro_officer = [OF].OF_PIN
                    AND RO_STATUS = 'INFO'
                    AND  RO_SHIFT_START between DATEADD(week, -12, GETDATE()) AND GETDATE() 
            GROUP BY  
                    RO_OFFICER,RO_ACTIVITY,Value) a
    JOIN
        (
            SELECT 
                    RO_OFFICER [RO_OFFICER] ,(ISNULL(SUM(RO_SHIFT_LENGTH - RO_BREAK_LENGTH), 0.0)/60.0)as [NumOfHoursWorkedInTheLast12Weeks],RSWH.dbo.RSWHF_GetMarker(RO_OFFICER, DATEADD(week, -12, GETDATE()), GETDATE()) AS Markers
            FROM 
                    ROLECALL.DBO.ROSTER as ro inner join [RSWH].DBO.[RSWHT_Hours_Assigned_To_Markers_LookUp] AS MR on RO_ACTIVITY = MR.MARKER
            WHERE 
                    RO_SHIFT_START >= DATEADD(week, -12, GETDATE()) AND RO_SHIFT_START <= GETDATE()
                    AND RO.RO_STATUS = 'INFO'
                    AND RO_OFFICER = [OF].OF_PIN
        GROUP BY 
                    RO_OFFICER
        ) b on a.RO_OFFICER = b.RO_OFFICER
        GROUP BY A.RO_OFFICER,b.NumOfHoursWorkedInTheLast12Weeks,Markers)

FROM
      RoleCall.dbo.OFFICER AS [OF] WITH (NOLOCK) 
      LEFT JOIN RoleCall.dbo.TEMPLATE AS [TO] WITH (NOLOCK) ON [TO].TO_PIN =  [OF].OF_PIN AND [TO].TO_TYPE COLLATE Latin1_General_CS_AS = 'M'
LEFT JOIN RoleCall.dbo.ACTIVITY AS AC WITH (NOLOCK) ON AC.AC_PIN = [TO].TO_ACTIVITY
LEFT JOIN RoleCall.dbo.ZONE AS ZN WITH (NOLOCK) ON ZN.ZN_ZONE = AC.AC_ZONE
LEFT JOIN RoleCall.dbo.AREA AS AR WITH (NOLOCK) ON AR.AR_AREA = AC.AC_AREA
LEFT JOIN Appollo.ACMS.dbo.ACSMT_Checked_Officer AS CO WITH (NOLOCK) ON CO.OfficerPin = [OF].OF_PIN AND WeekCommencing BETWEEN @WeekCommencing AND @WeekEnding
WHERE
[OF].OF_PAYROLL_NO IN ('W', 'S')    
AND [OF].OF_SKILLS = @Of_Skills --
AND (@Of_Skills = 'R' OR AC.AC_PARENT IN ('9947', '9133'))
AND ([OF].OF_DOL IS NULL OR [OF].OF_DOL > @WeekCommencing)
AND [OF].OF_RANK <> 'PT' 
AND [OF].OF_RANK <> 'LW' 
AND EXISTS (
                SELECT
                        TOP 1 Of_Pin
                FROM
                        HR.dbo.HRST_Contract AS HC WITH(NOLOCK)
                WHERE 
                        Of_Pin = [OF].OF_PIN
                        AND ISNULL(SD_Contract, 0) = 1
            )
AND         (
                SELECT 
                        ISNULL(SUM(RO.RO_SHIFT_LENGTH - RO_BREAK_LENGTH), 0.0)
                FROM 
                        RoleCall.dbo.ROSTER AS RO WITH(NOLOCK) 
                WHERE
                        RO.RO_OFFICER = [OF].OF_PIN
                        AND RO.RO_SHIFT_START BETWEEN @WeekCommencing AND @WeekEnding
                        AND (RO.RO_STATUS IN ('COMP', 'CONF', 'TODO') OR (RO_Activity ='LEAVE' AND RO_STATUS <> 'CANC'))
                ) < 420.0       
AND (ISNULL(CreatedBy, '') = '' OR @UncheckedOfficersOnly = 0)

ORDER BY
        Officer_Name, 
        Officer_Pin

问题是我需要返回我试图求和的三列。我没有多少SQL经验,我想知道是否有人有任何建议或指示要做。

谢谢

0 个答案:

没有答案