按星期几的特定列的总和

Sum of particular columns by day of week

我有自行车租赁数据,我使用 PostgreSQL 中的以下代码按每天的使用次数对其进行了分组:

SELECT date_trunc('day', rental_date) FROM rentalinfo;

SELECT
    COUNT(date_trunc('day', rental_date)) as counted_leads,
    date_trunc('day', rental_date) as count_date
FROM rentalinfo
GROUP BY date_trunc('day', rental_date)
ORDER BY date_trunc('day', rental_date) ASC;

结果给出一个名为 counted_leads 的列,其中包含每天的租赁数量。我想做一个查询,我可以在其中分别提取和总结周末和工作日的租金数量。我尝试了工作日:

SELECT SUM(counted_leads) AS sum_date WHERE count_date NOT IN ('2021-12-04',..)

但是我收到一条错误消息“错误:在“SELECT”处或附近出现语法错误”。

请问我该如何解决?

在 where 子句中使用 extract(dow ...) 过滤所有工作日(或周末)行并计算它们:

select count(*) as weekdays
from rentalinfo
where extract(dow from rental_date) in (1, 2, 3, 4, 5)

或者使用条件聚合:

select count(case when extract(dow from rental_date) in (1, 2, 3, 4, 5) then 1 end) as weekdays
     , count(case when extract(dow from rental_date) in (0, 6) then 1 end) as weekends
from rentalinfo