0
SELECT location, property_id, MIN(year_week) 
FROM mytable 
WHERE first_day='Mon' AND part_full_flag='F' 
GROUP BY location, property_id, full_week_start

这给了我一个假期属性的第一个可用日期列表,这些日期的开始日期是星期一。

该表还有另一列 - “full_week_start” - 它给出了格式为“DD-MM-YYYY”的确切开始日期

我现在要做的是仅显示开始日期在月初的结果。

如果我将 full_week_start 添加到查询中,它会显示从每月 1 日开始的第一行,而不仅仅是第一行,它是从每月 1 日开始的。

有任何想法吗?

编辑:样本数据

location | property_id | year_week | first_day | part_full_flag | full_week_start
---------------------------------------------------------------------------------
A        | 1           | 201434    | Mon       | F              | 18-08-2014
A        | 1           | 201435    | Mon       | F              | 25-08-2014
A        | 1           | 201436    | Mon       | F              | 01-09-2014
A        | 2           | 201434    | Mon       | F              | 18-08-2014
A        | 2           | 201435    | Mon       | F              | 25-08-2014
A        | 2           | 201449    | Mon       | F              | 01-12-2014

我只想从这些返回 2 行 - 看起来像这样......

location | property_id | MIN(year_week)
---------------------------------------
A        | 1           | 201436
A        | 2           | 201449

这是一个 SQL Fiddle... http://sqlfiddle.com/#!4/e9d64/3

4

3 回答 3

0

如果你这样做会发生什么:

SELECT location, property_id, MIN(year_week) 
FROM mytable 
WHERE first_day='Mon' 
AND part_full_flag='F' 
AND full_week_start like '01%'
GROUP BY location, property_id, full_week_start

更新:

SELECT location, property_id, MIN(year_week) 
FROM mytable
WHERE first_day='Mon' 
AND part_full_flag='F' 
AND property_id IN (SELECT distinct property_id FROM mytable where full_week_start like '01%')
GROUP BY location, property_id
于 2013-07-09T11:56:15.217 回答
0

这能满足您的需要吗?

SELECT location, property_id, MIN(year_week) 
FROM mytable 
WHERE first_day='Mon' AND part_full_flag='F' 
GROUP BY location, property_id, full_week_start
HAVING full_week_start LIKE '01%'
于 2013-07-09T12:00:20.037 回答
0
SELECT location, property_id, MIN(year_week) 
FROM mytable 
WHERE first_day='Mon' AND part_full_flag='F'
AND TO_CHAR(full_week_start,'MM/DD/YY') LIKE '01%'
GROUP BY location, property_id
于 2013-07-09T11:59:12.920 回答