我想通过传递日期直到到达最终日期来创建递归cte。谁能给我示范如何做的例子
我需要输入开始日期和结束日期,并计算出每年有多少学生!
其中:date和:finaldate是输入
with seventeen as
(
select count(studentid)
from students s
join periodrange p on p.idx = s.periodrangeidx
where p.validdate between :date and :finaldate
)
我想要每年的学生总数
像这样的事情还能奏效吗,或者我梦想到遥远的CTE
答案 0 :(得分:2)
您不需要CTE,只需使用分组依据并计数:
select count(studentid), extrac(year from p.validatdate) as Year
from students s
join periodrange p on p.idx = s.periodrangeidx
where p.validdate between :date and :finaldate
Group by extrac(year from p.validatdate), studentid