PL / SQL使用函数获取每行的结果

时间:2014-12-02 15:26:10

标签: sql oracle plsql

我刚刚编写了一个函数来根据平均颜色与输入颜色得到图像的分数。这样做的目的是我可以根据他们的得分与输入颜色的相关性来对图像进行排序。

该功能如下所示:

CREATE OR REPLACE FUNCTION get_average_color_score
(
    p_red               NUMBER,
    p_green             NUMBER,
    p_blue              NUMBER,
    p_input_image       store_images.game_id%TYPE
) 
    RETURN DOUBLE PRECISION
IS
    -- Local variables
    l_average_color     ORDSYS.SI_AverageColor; 
    l_image_to_eval     ORDSYS.SI_StillImage;
    l_input_color       ORDSYS.SI_Color;
    l_blob_reference    BLOB;

    -- Determines how well the image matches the given color (%)
    r_avg_score             DOUBLE PRECISION;
BEGIN
    -- select the thumbnail into the BLOB data column
    SELECT thumbnail 
    INTO   l_blob_reference
    FROM   store_images
    WHERE  image_id = p_input_image;

    -- Build the SI_Color Object
    l_input_color := SI_Color(p_red, p_green, p_blue);

    -- Create the SI_StillImage to evaluate 
    l_image_to_eval := new SI_StillImage(l_blob_reference);

    -- Get the average color score %
    l_average_color := NEW SI_AverageColor(l_input_color);
    r_avg_score := SI_ScoreByAvgClr(l_average_color, l_image_to_eval);

    -- Return the % score
    RETURN r_avg_score;
END get_average_color_score;

现在我希望能够使用一个查询评估所有图像的分数,并将它们从最相关到​​最不相关的列表中返回,但是在实现此查询的逻辑时,我似乎遇到了问题。 (可能是因为我过去3个小时都在查看相同的代码)。

编辑:我尝试使用子查询解决此问题:

SELECT * FROM 
(
  SELECT store_images.game_id, get_average_color_score(100,200,85, items.game_id) as score
  FROM store_images
  INNER JOIN items ON store_images.game_id = items.game_id
  WHERE items.store_id = 2
) ORDER BY score DESC;

然而,SQLDeveloper输出的结果是:

  GAME_ID  |  SCORE
---------------------
14          null
22          null
8           null
23          null
20          null

我期待的内容如下:

  GAME_ID  |  SCORE
---------------------
14          28.09345
22          33.44455
8           34.33345
23          52.03213
20          12.34543

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

您正在传递game_id并使用image_id选择缩略图,因此下面的sql正确

p_input_image       store_images.game_id%TYPE

-- select the thumbnail into the BLOB data column
SELECT thumbnail 
INTO   l_blob_reference
FROM   store_images
WHERE  image_id = p_input_image;

如果上述SQL正确,则更改您的查询以检查图像ID是否存在

SELECT * FROM 
(
 SELECT store_images.game_id, get_average_color_score(100,200,85, items.game_id) as score

FROM store_images
INNER JOIN items ON store_images.game_id = items.game_id
WHERE items.store_id = 2 
AND store_images.game_id in 
(select a.image_id from store_images a)
) ORDER BY score DESC;