使用原始sql或Django orm为给定月份的每一天获取5项
假设一个事件与一个日期关联 在一个月内 我想检索当月发生的事件, 具有这样的约束:使用原始sql或Django orm为给定月份的每一天获取5项,sql,django,database,postgresql,Sql,Django,Database,Postgresql,假设一个事件与一个日期关联 在一个月内 我想检索当月发生的事件, 具有这样的约束: 我每天最多只想要5个活动 大多数数据库支持ANSI标准窗口函数。因此,这将被写为: select t.* from (select t.*, row_number() over (partition by thedate order by thedate) as seqnum from t where
我每天最多只想要5个活动 大多数数据库支持ANSI标准窗口函数。因此,这将被写为:
select t.*
from (select t.*,
row_number() over (partition by thedate
order by thedate) as seqnum
from t
where date >= @date1 and date < @date2
) t
where seqnum <= 5
@date1和@date2只是定义您关心的时段的开始和结束的值的占位符。大多数数据库支持ANSI标准窗口函数。因此,这将被写为:
select t.*
from (select t.*,
row_number() over (partition by thedate
order by thedate) as seqnum
from t
where date >= @date1 and date < @date2
) t
where seqnum <= 5
@date1和@date2只是定义您所关心的时间段的开始和结束的值的占位符。请用您正在使用的基础数据库标记您的问题。@GordonLinoff:我刚刚做了。请用您正在使用的基础数据库标记您的问题。@GordonLinoff:我刚刚做了。谢谢您的快速回答。我不熟悉排号和查找它的含义。只是检查一下,seqnum oh nvm,我明白了。它返回标记小于5的成员,因此它是前5个成员。。谢谢,我明白了。谢谢你的快速回答。我不熟悉排号和查找它的含义。只是检查一下,seqnum oh nvm,我明白了。它返回标记小于5的成员,因此它是前5个成员。。谢谢,我明白了。