我有两个表RSLTS和联系方式:
RSLTS
QRY_ID | RES_ID | SCORE
-----------------------------
A | 1 | 15
A | 2 | 32
A | 3 | 29
C | 7 | 61
C | 9 | 30
联系
C_ID | QRY_ID | RES_ID
----------------------------
1 | A | 2
2 | A | 1
3 | C | 9
我试图创建一份报告,显示每个联系人记录( C_ID
)时, RANK()
的RES_ID
(由SCORE
其组内(在RSLTS表) QRY_ID
)。 使用上面的数据,它是这样的:
C_ID | QRY_ID | RES_ID | SCORE | Rank
-----------------------------------------------
1 | A | 2 | 32 | 1
2 | A | 1 | 15 | 3
3 | C | 9 | 30 | 2
到目前为止,我试过,但它返回等级= 1的最后一行(和秩= 2的第二个,这也是错误的)
SELECT
C.*
,R.SCORE
,RANK() OVER (PARTITION BY R.QRY_ID ORDER BY R.SCORE DESC)
FROM CONTACTS C LEFT JOIN RSLTS R
ON C.RES_ID = R.RES_ID
AND C.QRY_ID = R.QRY_ID
更新: SQLFiddle