这是 PostgreSQL 中视图的正确用法吗?
Is this the correct usage of a view in PostgreSQL?
我有两个 table。一个存储位置,另一个存储这些位置的不同评级(评级可以来自不同的用户,因此一个位置可以有多个评级)。我想要一种在平均评分旁边显示位置名称的方法。我相信我需要使用视图来创建它。这是我的 tables:
locations
有一个 name
属性 和一个 id
属性 这是我 ratings
[=36= 的外键] location_id
.
ratings
有一个 location_id
属性、一个 user_id
属性 和保持 eating
、[=20 评级的属性=]、child_friendly
、pet_friendly
和 accessible
.
这是我创建的视图:
CREATE VIEW v_location_averages AS
SELECT
r.location_id,
t.name,
t.eating,
t.reading,
t.child_friendly,
t.pet_friendly,
t.accessible,
(COALESCE(t.eating, 0) + COALESCE(t.reading, 0) + COALESCE(t.child_friendly, 0) + COALESCE(t.pet_friendly, 0) + COALESCE(t.accessible, 0)) / num_nonnulls(t.eating, t.reading, t.child_friendly, t.pet_friendly, t.accessible) AS "overall_rating"
FROM (
SELECT
locations.name,
location_id,
AVG(eating) AS "eating",
AVG(reading) AS "reading",
AVG(child_friendly) AS "child_friendly",
AVG(pet_friendly) AS "pet_friendly",
AVG(accessible) AS "accessible",
FROM ratings
INNER JOIN locations on ratings.location_id = locations.id
GROUP BY location_id, locations.name
) t
JOIN ratings r ON r.location_id = t.location_id
GROUP BY r.location_id, t.name, t.eating, t.reading, t.child_friendly, t.pet_friendly, t.accessible;
这是实现我想要的目标的正确方法吗?这是创建该视图的最佳方法吗? (确实有效,但正确吗?)
使用视图确实是一种很好的做法,但在您的情况下,视图可以更简单:
CREATE VIEW v_location_averages AS
SELECT
t.location_id,
l.name,
t.eating,
t.reading,
t.child_friendly,
t.pet_friendly,
t.accessible,
(COALESCE(t.eating, 0) + COALESCE(t.reading, 0) + COALESCE(t.child_friendly, 0) + COALESCE(t.pet_friendly, 0) + COALESCE(t.accessible, 0)) / num_nonnulls(t.eating, t.reading, t.child_friendly, t.pet_friendly, t.accessible) AS "overall_rating"
FROM (
SELECT
location_id,
AVG(eating) AS "eating",
AVG(reading) AS "reading",
AVG(child_friendly) AS "child_friendly",
AVG(pet_friendly) AS "pet_friendly",
AVG(accessible) AS "accessible",
FROM ratings
GROUP BY location_id
) t
INNER JOIN locations l on t.location_id = l.id;
我有两个 table。一个存储位置,另一个存储这些位置的不同评级(评级可以来自不同的用户,因此一个位置可以有多个评级)。我想要一种在平均评分旁边显示位置名称的方法。我相信我需要使用视图来创建它。这是我的 tables:
locations
有一个name
属性 和一个id
属性 这是我ratings
[=36= 的外键]location_id
.ratings
有一个location_id
属性、一个user_id
属性 和保持eating
、[=20 评级的属性=]、child_friendly
、pet_friendly
和accessible
.
这是我创建的视图:
CREATE VIEW v_location_averages AS
SELECT
r.location_id,
t.name,
t.eating,
t.reading,
t.child_friendly,
t.pet_friendly,
t.accessible,
(COALESCE(t.eating, 0) + COALESCE(t.reading, 0) + COALESCE(t.child_friendly, 0) + COALESCE(t.pet_friendly, 0) + COALESCE(t.accessible, 0)) / num_nonnulls(t.eating, t.reading, t.child_friendly, t.pet_friendly, t.accessible) AS "overall_rating"
FROM (
SELECT
locations.name,
location_id,
AVG(eating) AS "eating",
AVG(reading) AS "reading",
AVG(child_friendly) AS "child_friendly",
AVG(pet_friendly) AS "pet_friendly",
AVG(accessible) AS "accessible",
FROM ratings
INNER JOIN locations on ratings.location_id = locations.id
GROUP BY location_id, locations.name
) t
JOIN ratings r ON r.location_id = t.location_id
GROUP BY r.location_id, t.name, t.eating, t.reading, t.child_friendly, t.pet_friendly, t.accessible;
这是实现我想要的目标的正确方法吗?这是创建该视图的最佳方法吗? (确实有效,但正确吗?)
使用视图确实是一种很好的做法,但在您的情况下,视图可以更简单:
CREATE VIEW v_location_averages AS
SELECT
t.location_id,
l.name,
t.eating,
t.reading,
t.child_friendly,
t.pet_friendly,
t.accessible,
(COALESCE(t.eating, 0) + COALESCE(t.reading, 0) + COALESCE(t.child_friendly, 0) + COALESCE(t.pet_friendly, 0) + COALESCE(t.accessible, 0)) / num_nonnulls(t.eating, t.reading, t.child_friendly, t.pet_friendly, t.accessible) AS "overall_rating"
FROM (
SELECT
location_id,
AVG(eating) AS "eating",
AVG(reading) AS "reading",
AVG(child_friendly) AS "child_friendly",
AVG(pet_friendly) AS "pet_friendly",
AVG(accessible) AS "accessible",
FROM ratings
GROUP BY location_id
) t
INNER JOIN locations l on t.location_id = l.id;