我在PostgreSQL中查询遇到麻烦。
我正在尝试将两个查询的结果相除而未成功。
with s1 as (
select count(*)
from university.sevent as p3
where p3.edate not in (
select p1.edate
from university.sevent as p1
join university.semester as p2
on p1.edate between p2.sdate and p2.edate
)
),
s2 as (
select count(p1.edate)
from university.sevent as p1
join university.semester as p2
on p1.edate between p2.sdate and p2.edate
)
select div(s1,s2);
但我所得到的是它无法识别s1
或s2
。
答案 0 :(得分:1)
s1
和s2
是表的名称,但是您需要列名称:
div(s1.count, s2.count) FROM s1 CROSS JOIN s2
但是我认为您不需要div
函数,因为两个字段都是bigint
。您可以只使用s1.count / s2.count
。
答案 1 :(得分:0)
WITH查询提供了一种编写辅助语句以用于较大查询的方法。它有助于将复杂的大型查询分解为更易于阅读的更简单形式。这些语句通常称为共同表,作为结果s1和s2两个差异表
with s1 as (
select count(*) as C1
from university.sevent as p3
where p3.edate not in (
select p1.edate
from university.sevent as p1
join university.semester as p2
on p1.edate between p2.sdate and p2.edate
)
),
s2 as (
select count(p1.edate) as C2
from university.sevent as p1
join university.semester as p2
on p1.edate between p2.sdate and p2.edate
)
select C1/C2 from s1,s2;