我正在使用 SQL SERVER 2008。我有以下查询:
SELECT Row_number()
OVER (
partition BY ru.rul_icontfk
ORDER BY rut.rut_isortorder, cnt.ctr_caltcode3, rut.rut_crultype,
ru.rul_ntiermax) AS order1,
ru.rul_ipk,
ru.rul_icontfk,
ru.rul_crultype,
ru.rul_cisbn,
ru.rul_crecipid,
ru.rul_ccalcmeth,
ru.rul_ctiertype,
ru.rul_ntiermax
FROM rules.ru
INNER JOIN ruletype rut
ON rut.rut_crultype = ru.rul_crultype
INNER JOIN country cnt
ON cnt.ctr_cisocode = ru.rul_cctry
WHERE ru.rul_icontfk = '108122' --and rul_ipk=1227754
ORDER BY order1
+--------+---------+-------------+--------------+---------------+--------------+---------------+---------------+---------------+
| order1 | rul_ipk | rul_icontfk | rul_crultype | rul_cisbn | rul_crecipid | rul_ccalcmeth | rul_ctiertype | rul_ntiermax |
+--------+---------+-------------+--------------+---------------+--------------+---------------+---------------+---------------+
| 1 | 1227752 | 108122 | 1 | 9780415910866 | S565154 | N | LU | 9999999 |
| 2 | 1227758 | 108122 | 1 | 9780415910866 | S565154 | N | LU | 9999999 |
| 3 | 1227754 | 108122 | 1 | 9780415910866 | S565154 | N | LU | 9999999 |
| 4 | 1227759 | 108122 | 2 | 9780415910866 | S565154 | N | LU | 9999999 |
| 5 | 1227755 | 108122 | 2 | 9780415910866 | S565154 | N | LU | 9999999 |
| 6 | 1227753 | 108122 | 3 | 9780415910866 | S565154 | N | LU | 9999999 |
| 7 | 1227760 | 108122 | 3 | 9780415910866 | S565154 | N | LU | 9999999 |
| 8 | 1227756 | 108122 | 3 | 9780415910866 | S565154 | N | LU | 9999999 |
| 9 | 1227761 | 108122 | S | 9780415910866 | S565154 | N | LU | 9999999 |
| 10 | 1227762 | 108122 | T | 9780415910866 | S565154 | N | LU | 9999999 |
| 11 | 1227757 | 108122 | 4 | 9780415910866 | S565154 | N | LU | 9999999 |
+--------+---------+-------------+--------------+---------------+--------------+---------------+---------------+---------------+
我想知道如何获取 rul_ipk > 1227754 的记录,它们的 order1 > 3
SELECT *
FROM (SELECT *,
Row_number()
OVER (
ORDER BY rut.rut_isortorder, cnt.ctr_caltcode3,
rut.rut_crultype,
RU.rul_ntiermax) AS row
FROM rules ru
INNER JOIN ruletype rut
ON rut.rut_crultype = ru.rul_crultype
INNER JOIN country cnt
ON cnt.ctr_cisocode = ru.rul_cctry
WHERE ru.rul_icontfk = '108122') a
WHERE row > 3
+---------+-------------+
| rul_ipk | rul_icontfk |
+---------+-------------+
| 1227759 | 108122 |
| 1227755 | 108122 |
| 1227753 | 108122 |
| 1227760 | 108122 |
| 1227756 | 108122 |
| 1227761 | 108122 |
| 1227762 | 108122 |
| 1227757 | 108122 |
+---------+-------------+
我尝试使用此查询获取结果以获取记录号,但我得到 order1 = 1
SELECT Row_number()
OVER (
partition BY ru.rul_icontfk
ORDER BY rut.rut_isortorder, cnt.ctr_caltcode3, rut.rut_crultype,
RU.rul_ntiermax) AS order1,
RU.rul_ipk,
RU.rul_icontfk,
RU.rul_crultype,
RU.rul_cisbn,
RU.rul_crecipid,
RU.rul_ccalcmeth,
RU.rul_ctiertype,
RU.rul_ntiermax
FROM rules ru
INNER JOIN ruletype rut
ON rut.rut_crultype = ru.rul_crultype
INNER JOIN country cnt
ON cnt.ctr_cisocode = ru.rul_cctry
WHERE ru.rul_icontfk = '108122'
AND rul_ipk = 1227754
ORDER BY order1
order1 rul_ipk rul_icontfk rul_crultype 1 1227754 108122 1 我试过这个查询 选择 * 从 ( SELECT *, ROW_NUMBER() OVER (ORDER BY rut.rut_isortorder, cnt.ctr_caltcode3, rut.rut_crultype, RU.rul_ntiermax) 作为行 从规则汝 rut.rut_crultype = ru.rul_crultype 上的内部连接规则类型 rut 在 cnt.ctr_cisocode = ru.rul_cctry 上内连接国家 cnt 哪里 ru.rul_icontfk = '108122' ) 一个 WHERE 行 > (SELECT rownum.order1 from (select row_number() over (partition by ru.rul_icontfk order by rut.rut_isortorder, cnt.ctr_caltcode3, rut.rut_crultype, RU.rul_ntiermax) 作为 order1, RU.rul_ipk, RU.rul_icontfk, RU.rul_crultype, RU.rul_cisbn, RU.rul_crecipid、RU.rul_ccalcmeth、RU.rul_ctiertype、RU.rul_ntiermax 从规则汝 rut.rut_crultype = ru.rul_crultype 上的内部连接规则类型 rut 在 cnt.ctr_cisocode = ru.rul_cctry 上内连接国家 cnt 哪里 ru.rul_icontfk = '108122' ) 作为 rownum 其中 rul_ipk=1227754) 它给了我想要的结果,但我不知道是否有最简单的方法来做到这一点 rul_ipk rul_icontfk rul_crultype 1227759 108122 2 1227755 108122 2 1227753 108122 3 1227760 108122 3 1227756 108122 3 1227761 108122 小号 1227762 108122 1227757 108122 4