我尝试在SQL Server 2005中使用以下代码(它是在2012年编写的):
SELECT *
FROM (SELECT TOP (100) PERCENT ID_BADGE,
DATE_TRX,
CODE_TRX,
SEQ_REC
FROM omi.DCUTRX_ZERO
UNION ALL
SELECT TOP (100) PERCENT ID_BADGE,
DATE_TRX,
CODE_TRX,
SEQ_REC
FROM omi.TAHIST_ZERO) OMI
WHERE ( ID_BADGE = ' ' + @Badge )
AND ( DATE_TRX BETWEEN @Date AND @Date2 )
AND ( SEQ_REC IN ( '0', '1000' ) )
AND ( CODE_TRX IN ( '100', '101' ) )
OFFSET 1 ROWS
FETCH NEXT 2 ROWS ONLY
有人能告诉我SQL Server 2005的等价物吗?
答案 0 :(得分:2)
SELECT ID_BADGE, DATE_TRX, CODE_TRX, SEQ_REC
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY DATE_TRX) rn
FROM (
SELECT ID_BADGE, DATE_TRX, CODE_TRX, SEQ_REC
FROM omi.DCUTRX_ZERO
UNION ALL
SELECT ID_BADGE, DATE_TRX, CODE_TRX, SEQ_REC
FROM omi.TAHIST_ZERO
) q
WHERE ID_BADGE = ' ' + @Badge
AND DATE_TRX BETWEEN @Date AND @Date2
AND SEQ_REC IN ('0', '1000')
AND CODE_TRX IN ('100', '101')
) q
WHERE rn BETWEEN 2 AND 3
ORDER BY
DATE_TRX
请注意,SQL Server中需要某种ORDER BY
才能使ROW_NUMBER
正常工作。