Sql 如何合并这两个查询
我需要将这两个查询合并到一个查询中 第一个问题:Sql 如何合并这两个查询,sql,sql-server,database,sql-server-2012,query-optimization,Sql,Sql Server,Database,Sql Server 2012,Query Optimization,我需要将这两个查询合并到一个查询中 第一个问题: sSQLO = " SET NOCOUNT ON " & _ " SELECT TOP 1 dbo.tblScheduleExaminees.formAdministerID, dbo.tblBatteryToExams.testOrder, " & _ " dbo.tblScheduleExaminees.takenNumber " & _ " FROM dbo.tblSc
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 "
第二个问题:
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 "
这些查询是相同的
dbo.TBLScheduleOccessees.takenNumber
通过第一次查询选择ed,第二次查询使用名称iTakenNumber
对不起,我遗漏了一些内容。”第二个查询需要的takenNumber是“takenNumber”+1。并且还需要由第一个查询选择的另一个值(dbo.tblschedulexaminers.formAdministerID、dbo.tblBatteryToExams.testOrder)。请考虑2个查询之间的“订单”差异,你的问题我不清楚。请在您的问题中提供更详细的信息。这是我想问您的问题:(1)第一个查询中的“takenNumber”应该在第二个查询使用之前加上1(2)还需要另一个由第一个查询选择的值(dbo.tblschedulexecasures.formAdministerID、dbo.tblBatteryToExams.testOrder)。因此,请将其包含在SELECT上
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