我有桌子:
City | Start_date
---------------------------
London | 2015-01-01
Paris | 2015-02-15
Moscov | 2015-05-13
Sydney | 2015-07-13
Berlin | 2015-09-03
New York | 2015-09-28
Los Angeles | 2015-10-15
San Francisco | 2015-10-29
我创建了查询:
Select City, Start_date
from Table
where Start_date between '2015-06-01' and '2015-09-30'
我有输出:
City | Start_date
---------------------
Sydney | 2015-07-13
Berlin | 2015-09-03
New York | 2015-09-28
但我想要输出:
City | Start_date
---------------------
Moscov | 2015-05-13
Sydney | 2015-07-13
Berlin | 2015-09-03
New York | 2015-09-28
在我的查询中,我没有莫斯科。我喜欢创建查询以从日期之间的用户出现的范围日期开始占用所有城市。
答案 0 :(得分:0)
试试这个:
Select a.City, a.Start_date
from Table a
left join table b
on b.Start_Date =
(Select Min(Start_Date)
from table
where Start_Date > a.Start_Date)
where a.Start_date <= '2015-09-30'
and b.start_Date >= '2015-06-01'
答案 1 :(得分:0)
您需要通过将表格连接到自身并选择开始日期之后的日期分钟并减去1天来创建结束日期列。
SELECT Table1.StartDate, MIN(Table2.StartDate) - 1 AS EndDate
FROM Table1
INNER JOIN Table2
ON Table1.StartDate < Table2.StartDate
GROUP BY Table1.StartDate
HAVING Table1.StartDate <= [Time Period End Date]
AND (MIN(Table2.StartDate) - 1) >= [Time Period Start Date]