我正在使用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 I tried this query 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 > (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) 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' ) as rownum where rul_ipk=1227754) which it give me the result desired but I don't know if there is a easiest way to do this rul_ipk rul_icontfk rul_crultype 1227759 108122 2 1227755 108122 2 1227753 108122 3 1227760 108122 3 1227756 108122 3 1227761 108122 S 1227762 108122 T 1227757 108122 4
答案 0 :(得分:0)
您可以使用两个行号创建一个选择,然后使用CTE比较其中的两个数字
WITH CTE AS
(SELECT
Row_number()
OVER (
ORDER BY rut.rut_isortorder, cnt.ctr_caltcode3,
rut.rut_crultype,
RU.rul_ntiermax) AS row,
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 )
SELECT * FROM CTE
WHERE order1 > row
答案 1 :(得分:0)
The results of the columns row and order1 are 1.
I modified the query to get the results desired
WITH CTE AS
(SELECT
Row_number()
OVER (
ORDER BY rut.rut_isortorder, cnt.ctr_caltcode3,
rut.rut_crultype,
RU.rul_ntiermax) AS row,
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 )
Select * from Cte where row > (Select row from cte where rul_ipk = 1227754 )
row order1 rul_ipk rul_icontfk
4 4 1227759 108122
5 5 1227755 108122
6 6 1227753 108122
7 7 1227760 108122
8 8 1227756 108122
9 9 1227761 108122
10 10 1227762 108122
11 11 1227757 108122
----> These are the results for each condition:
Select row from cte where rul_ipk = 1227754
row
3
Select * from Cte
row order1 rul_ipk rul_icontfk
1 1 1227752 108122
2 2 1227758 108122
3 3 1227754 108122
4 4 1227759 108122
5 5 1227755 108122
6 6 1227753 108122
7 7 1227760 108122
8 8 1227756 108122
9 9 1227761 108122