Symfony2查询从Holiday Calender查找最后的工作日期

时间:2016-02-17 09:24:02

标签: postgresql symfony datetime orm doctrine

我的项目中有一个日历实体,负责管理全年营业日的开放和关闭时间。 以下是特定月份的记录

id   |     today_date      | year | month_of_year | day_of_month  | is_business_day
-------+---------------------+------+---------------+-------------+---------------+         
10103 | 2016-02-01 00:00:00 | 2016 |             2 |            1 | t 
10104 | 2016-02-02 00:00:00 | 2016 |             2 |            2 | t
10105 | 2016-02-03 00:00:00 | 2016 |             2 |            3 | t
10106 | 2016-02-04 00:00:00 | 2016 |             2 |            4 | t
10107 | 2016-02-05 00:00:00 | 2016 |             2 |            5 | t
10108 | 2016-02-06 00:00:00 | 2016 |             2 |            6 | f
10109 | 2016-02-07 00:00:00 | 2016 |             2 |            7 | f
10110 | 2016-02-08 00:00:00 | 2016 |             2 |            8 | t
10111 | 2016-02-09 00:00:00 | 2016 |             2 |            9 | t
10112 | 2016-02-10 00:00:00 | 2016 |             2 |           10 | t
10113 | 2016-02-11 00:00:00 | 2016 |             2 |           11 | t
10114 | 2016-02-12 00:00:00 | 2016 |             2 |           12 | t
10115 | 2016-02-13 00:00:00 | 2016 |             2 |           13 | f
10116 | 2016-02-14 00:00:00 | 2016 |             2 |           14 | f
10117 | 2016-02-15 00:00:00 | 2016 |             2 |           15 | t
10118 | 2016-02-16 00:00:00 | 2016 |             2 |           16 | t
10119 | 2016-02-17 00:00:00 | 2016 |             2 |           17 | t
10120 | 2016-02-18 00:00:00 | 2016 |             2 |           18 | t

我希望获得过去7个工作日期的today_date。支持today_date为2016-02-18,过去7个工作日期的日期为2016-02-09

2 个答案:

答案 0 :(得分:1)

您可以像这样使用row_number():

SELECT * FROM
(SELECT t.*,row_number() OVER(order by today_date desc) as rnk
FROM Calender t
WHERE today_date <= current_date 
      AND is_business_day = 't')
WHERE rnk = 7

这将为您提供从今天起第7个工作日的行

答案 1 :(得分:0)

我看到您使用Doctrine,ORM和Datetime标记了您的问题。你是在使用QueryBuilder解决方案吗?也许这更接近你想要的东西:

$qb->select('c.today_date')
->from(Calendar::class, 'c')
->where("c.today_date <= :today")
->andWhere("c.is_business_day = 't'")
->setMaxResults(7)
->orderBy("c.today_date", "DESC")
->setParameter('today', new \DateTime('now'), \Doctrine\DBAL\Types\Type::DATETIME));