PostgreSQL div函数不起作用

时间:2018-08-03 06:47:52

标签: sql postgresql

我在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);

但我所得到的是它无法识别s1s2

2 个答案:

答案 0 :(得分:1)

s1s2是表的名称,但是您需要列名称:

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;