在我的数据库中,对于没有经销商评级的商店,他们仍然有一个条目,但有
-1.0
,而不是 0.0
和 10.0
之间的数字。对于那些带有 -10.00
的商店,以下查询结果会在我的视图中显示 -1.0
。相反,我希望什么都没有,或者在它的位置出现一个 -
,但在我看来,我不太愿意实现嵌入的 if 语句。这是我目前的看法。
CREATE VIEW myview AS
SELECT co_url_name AS company_url, score_combined AS stella_score, trunc(score*10, 2) AS bizrate_score,
(SELECT trunc("lifetimeRating"*10, 2)) AS resellerRating_score
FROM ss_profile_co AS s LEFT OUTER JOIN "resellerRatings_ratings" AS r
ON s.id = r.company_id
LEFT OUTER JOIN (SELECT * FROM bizrate_bizrate_ratings WHERE score_name = 'Overall rating') AS b
ON s.id = b.fk_co_id
ORDER BY co_url_name ASC;
第
(SELECT trunc("lifetimeRating"*10, 2)) AS resellerRating_score
行返回负数(或者,对于有效条目,将返回介于 0.00
和 100.00
之间的分数)。
显然,我可以简单地从数据库中删除这些条目,从而导致这种情况,但这一半是一种学习经历,一半是我无法做到的。
我感谢您的帮助!
编辑:尝试嵌入 if 但毫不奇怪地出现错误。
IF (SELECT trunc("lifetimeRating"*10, 2)) = -10.00 THEN NULL ELSE (SELECT trunc("lifetimeRating"*10, 2)) AS resellerRating_score
EDIT2:弄清楚了。有问题的行如下:
(SELECT trunc("lifetimeRating"*10, 2) WHERE trunc("lifetimeRating"*10, 2) > 0) AS resellerrating_score
/永远孤独
可能看起来像这样:
CREATE VIEW myview AS
SELECT co_url_name AS company_url
, score_combined AS stella_score
, trunc(score * 10, 2) AS bizrate_score
, CASE WHEN "lifetimeRating" < 0
THEN NULL
ELSE trunc("lifetimeRating" * 10, 2)
END AS resellerRating_score
FROM ss_profile_co s
LEFT JOIN "resellerRatings_ratings" r ON r.company_id = s.id
LEFT JOIN bizrate_bizrate_ratings b ON b.score_name = 'Overall rating'
AND b.fk_co_id = s.id
ORDER BY co_url_name;
没有
FROM
子句的子选择没有任何作用。我使用 CASE
语句来代替。
我还将您的
LEFT JOIN
简化为 bizrate_bizrate_ratings
。也不需要子选择。我将 WHERE
子句拉入 JOIN
条件。更简单、更快。
我建议不要使用混合大小写标识符,所以你永远不必使用双引号。 (这可能会使@Daniels 评论无效,因为
lifetimerating
!= "lifetimeRating"
)