我要做的是一个简单的推荐者,必须采用最大加权的前40个元素的node2元素。权重的计算来自(E.WEIGHT * K.GRADE)
。现在,此代码成功返回前40个元素。但是,我不希望E.NODE2
返回重复项。 POSTGRE SQL允许我做SELECT DISTINCT ON (NODE2) E.NODE2 , (E.WEIGHT * K.GRADE)
。我怎样才能在oracle中做同样的事情?
完整的SQL查询;
SELECT *
FROM (SELECT DISTINCT E.NODE2 , (E.WEIGHT * K.GRADE)
FROM KUAISFAST K, EDGES E
WHERE K.ID = 1 AND K.COURSE_ID = E.NODE1 AND E.NODE2 NOT IN(
SELECT K2.COURSE_ID
FROM KUAISFAST K2
WHERE K2.ID = 1
)
ORDER BY( E.WEIGHT * K.GRADE ) DESC) TEMP
WHERE rownum <= 40
答案 0 :(得分:1)
我相信你想要像
这样的东西SELECT *
FROM (
SELECT
E.NODE2,
(E.WEIGHT * K.GRADE),
ROW_NUMBER() OVER (PARTITION BY E.NODE2 ORDER BY E.WEIGHT * K.GRADE DESC) R
FROM
KUAISFAST K,
EDGES E
WHERE
K.ID = 1 AND
K.COURSE_ID = E.NODE1 AND
E.NODE2 NOT IN
( SELECT K2.COURSE_ID
FROM KUAISFAST K2
WHERE K2.ID = 1
)
ORDER BY (E.WEIGHT * K.GRADE) DESC
) TEMP
WHERE R=1 AND
ROWNUM <= 40
答案 1 :(得分:1)
在您的子选择中,我认为您需要:MAX(E.WEIGTH * K.GRADE)
以便每个E.NODE2
只返回一个值。
这意味着您还需要GROUP BY E.NODE2
。
答案 2 :(得分:1)
这应该可以解决你的问题,但速度很慢
SELECT * FROM
(SELECT *
FROM (SELECT E.NODE2 , max(E.WEIGHT * K.GRADE ) AS MAXDE
FROM KUAISFAST K, EDGES E
WHERE K.ID = 1 AND K.COURSE_ID = E.NODE1 AND E.NODE2 NOT IN(
SELECT K2.COURSE_ID
FROM KUAISFAST K2
WHERE K2.ID = 1
)
GROUP BY E.NODE2 )
ORDER BY MAXDE DESC)
WHERE rownum <= 40