Sql 如何在每天添加的新用户旁边添加运行的用户总数?
我试图展示今天新增的用户,以及总数 这个 输出此Sql 如何在每天添加的新用户旁边添加运行的用户总数?,sql,postgresql,date,group-by,count,Sql,Postgresql,Date,Group By,Count,我试图展示今天新增的用户,以及总数 这个 输出此 Date. New Total "2020-04-21" 50 50 "2020-04-20" 40 40 "2020-04-19" 30 30 "2020-04-18" 20 20 "2020-04-17" 10 10 如何在“总计”列中显示用户总数(到目前为止)?第21行应该是150行。您可以使用窗口函数来实现这一点:基本上,您需要每组行数的窗口总和,如: select da
Date. New Total
"2020-04-21" 50 50
"2020-04-20" 40 40
"2020-04-19" 30 30
"2020-04-18" 20 20
"2020-04-17" 10 10
如何在“总计”列中显示用户总数(到目前为止)?第21行应该是150行。您可以使用窗口函数来实现这一点:基本上,您需要每组行数的窗口总和,如:
select
date("CreatedAt") created_day,
count(*) "New",
sum(count(*)) over(order by date("CreatedAt")) "Total"
from "User"
group by date("CreatedAt")
order by created_day desc
select
date("CreatedAt") created_day,
count(*) "New",
sum(count(*)) over(order by date("CreatedAt")) "Total"
from "User"
group by date("CreatedAt")
order by created_day desc