2016-02-17 39 views
1

我在我的項目中有一個日曆實體,它管理全年營業日的開啓和關閉時間。 下面是我想要得到最後7個工作日期today_date特定月份Symfony2查詢來自假日日曆的最後一次工作日期

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 

的記錄。 supplied today_date爲2016-02-18,最近7個工作日期的日期爲2016-02-09

回答

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個營業日的行日期

0

我看到你學說標記你的問題, ORM和日期時間。你是在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));