Search code examples
sqlsql-serverdatabasesql-server-2012query-optimization

How to merge these 2 queries


I need to merge these 2 queries into a single query.

First query :

sSQLO = " SET NOCOUNT ON " & _
        " SELECT TOP 1 dbo.tblScheduleExaminees.formAdministerID, dbo.tblBatteryToExams.testOrder, " & _
        " dbo.tblScheduleExaminees.takenNumber " & _
        " FROM dbo.tblScheduleExaminees WITH (NOLOCK) " & _
        " INNER JOIN dbo.tblBatteryToExams WITH (NOLOCK) ON (dbo.tblBatteryToExams.schID = dbo.tblScheduleExaminees.schID) " & _
        " INNER JOIN dbo.tblBattery WITH (NOLOCK) ON ((dbo.tblBattery.batteryID = dbo.tblBatteryToExams.batteryID) AND (dbo.tblBattery.isDeleted  = 0) AND (dbo.tblBattery.isExpired = 0)) " & _
        " WHERE dbo.tblScheduleExaminees.isDeleted = 0 " & _
        "   AND dbo.tblScheduleExaminees.examineeID = " & pExamineeID & _
        "   AND dbo.tblScheduleExaminees.areaOption = " & iAreaID & _
        "   AND dbo.tblScheduleExaminees.confirmCheckIn = 1 " & _
        " ORDER BY dbo.tblScheduleExaminees.takenNumber DESC " & _
        " SET NOCOUNT OFF "

Second query:

sSQLC = " SET NOCOUNT ON  " & _
        " SELECT TOP 1 dbo.tblScheduleExaminees.areaOption " & _
        " FROM dbo.tblScheduleExaminees WITH (NOLOCK) " & _
        " INNER JOIN dbo.tblBatteryToExams WITH (NOLOCK) ON dbo.tblBatteryToExams.schID = dbo.tblScheduleExaminees.schID " & _
        " AND dbo.tblBatteryToExams.ssScore = '' " & _
        " INNER JOIN dbo.tblBattery WITH (NOLOCK) ON ((dbo.tblBattery.batteryID = dbo.tblBatteryToExams.batteryID) AND (dbo.tblBattery.isDeleted  = 0) AND (dbo.tblBattery.isExpired = 0)) " & _
        " WHERE dbo.tblScheduleExaminees.examineeID = " & pExamineeID & _
        "   AND dbo.tblScheduleExaminees.areaOption = " & iAreaID & _
        "   AND dbo.tblScheduleExaminees.takenNumber = " & iTakenNumber & _
        "   AND dbo.tblScheduleExaminees.isDeleted = 0 " & _
        "   AND dbo.tblScheduleExaminees.confirmCheckIn IN (0,1) " & _
        " ORDER BY dbo.tblScheduleExaminees.checkInDate DESC " & _
        " SET NOCOUNT OFF "

Those queries are identical. dbo.tblScheduleExaminees.takenNumber which SELECT-ed by first query are used by second query by the name iTakenNumber


Solution

  • SELECT TOP (1) e.areaOption
    FROM (
        SELECT
              e.areaOption
            , e.takenNumber
            , be.ssScore
            , e.checkInDate
            , MaxTakenNumber = MAX(CASE WHEN e.confirmCheckIn = 1 THEN takenNumber END) OVER (ORDER BY takenNumber DESC)
        FROM dbo.tblScheduleExaminees e
        JOIN dbo.tblBatteryToExams be ON be.schID = e.schID
        WHERE e.examineeID = @pExamineeID
            AND e.areaOption = @iAreaID
            AND e.isDeleted = 0
            AND e.confirmCheckIn IN (0, 1)
            AND EXISTS(
                    SELECT 1
                    FROM dbo.tblBattery b
                    WHERE b.batteryID = be.batteryID
                        AND b.isDeleted = 0
                        AND b.isExpired = 0
                )
    ) t
    WHERE t.MaxTakenNumber = t.takenNumber
        AND t.ssScore = ''
    ORDER BY t.checkInDate DESC