postgrePostgres (SQL)查询未保留天数



假设我有一个名为booking的表

id (PK)
class (int FK)
name (varchar 64)
start_date (timestamp)
end_date (timestamp)
...etc

对于这一年有许多预订,我要查找的是预订有效的总天数。例如,1月01日至1月05日和1月10日至1月12日将共7天,它将排除1月06日至1月09日…有效期为XX个月。

我假设class是保留的指示。因此,该查询将返回过去12个月的总预留天数。

select class, sum(end_date-start_date)
from table
where start_date between current_date + (-12::int || ' month')::interval and 
current_date
group by class

最新更新