[英]PostgreSQL: Counting Distinct Across Multiple Columns (Alongside Other Selections)
A simple SQL statement in PostgreSQL: PostgreSQL中的一个简单的SQL语句:
select class_offerings.semester_code as SC, class_offerings.class_code as CC,
class_offerings.class_name as CN, class_offerings.teacher_name as TN,
count(distinct class_enrollment.semester_code,
class_enrollment.class_code) as num_enrolled, --wrong
class_enrollment.maximum_capacity as max_enrollment
from class_offerings natural join class_enrollment;
PostgreSQL doesn't support this syntax. PostgreSQL不支持此语法。 What should I do instead?
我该怎么办呢? Expected result:
预期结果:
SC | CC | CN | TN | num_enrolled | max_enrollment
Never use natural join
. 切勿使用
natural join
。 It is an abomination, because it depends on columns having the same name. 这是可憎的,因为它取决于具有相同名称的列。 It doesn't even use properly declared foreign key relationships.
它甚至不使用正确声明的外键关系。
Table aliases make the query easier to write and to read. 表别名使查询更易于编写和阅读。 Perhaps you intend:
也许您打算:
select o.semester_code as SC, o.class_code as CC, o.class_name as CN, o.teacher_name as TN,
count(*) as num_enrolled, --wrong
from class_offerings o join
class_enrollment e
on c.? = e.?
group by o.semester_code, o.class_code, o.class_name, o.teacher_name;
Put the columns for the join in the on
clause where the ?
将用于连接的列放在
on
子句中,其中?
s are. 是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.