(十八)公式表达式

--查询表的时候,有时候中间表需要重复使用,这些子查询被重复查询调用,不但效率低,而且可读性低,不利于理解。那么公式表表达式可以解决这个问题。

--我们可以将公式表表达式(CET)视为临时结果集,在select、insert、update、delete或是create view语句的执行范围内进行定义。

--表达式
with statNum(id, num) as 
(
    select cid, count(*) 
    from studentB 
    where id > 0
    group by cid
)
select id, num from statNum order by id;
 
with statNum(id, num) as 
(
    select cid, count(*) 
    from studentB 
    where id > 0
    group by cid
)
select max(id), avg(num) from statNum;

 

posted @ 2025-02-18 16:33  代号六零一  阅读(14)  评论(0)    收藏  举报