-1

我正在使用 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
4

2 回答 2

1

您可以使用两个行号创建一个选择,然后使用 CTE 在 where 中比较这两个数字

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
于 2013-09-11T20:00:19.913 回答
0
列 row 和 order1 的结果为 1。

我修改了查询以获得所需的结果


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




于 2013-09-12T15:58:40.893 回答