[英]Oracle date between list of min max dates
我有一张在不同日期吃过的鱼。
桌台:
Long ID,
Long Vessel_id, -- vessel that is fishing
TimeStamp fishing_date,
double amount_cought
Long species_id;
接下来是一张使用船只捕鱼的公司表。
表ves_usage:
Long ID,
TimeStamp usage_start_date,
TimeStamp usage_end_date,
Long vessel_id,
Long using_company_id;
最后,我有一张在给定年份内为公司捕捞特定物种的许可证表。
餐桌牌照:
Long ID,
int year, --the year that the lincence is active
Long licenced_company_id,--company who was given the licence
Long species_id; --species that the company can fish
我正在尝试从着陆表中选择具有指定物种,公司和船只的所有记录。 我的问题是我想不出一种方法来选择ves_usage表记录的开始日期和结束日期之间的所有着陆记录。 不知道即时消息是否足够清晰:PI想做出类似的事情:
Select *
from landings
where fishing_date between <list of usage_start_date and usage_end_date from ves_usage table>
如果您希望查询基于ves_usage
ID
,则此查询将起作用:
SELECT *
FROM landings
WHERE fishing_date BETWEEN
(SELECT usage_start_date FROM ves_usage WHERE id = <id from ves_usage>)
AND
(SELECT usage_end_date FROM ves_usage WHERE id = <id from ves_usage>)
如果您希望有所不同,请根据评论中的要求进行指定。
如果我正确地遵循您的表,我相信查询将是这样的:
SELECT l.*
FROM landings l
JOIN ves_usage vu
ON vu.vessel_id = l.Vessel_id
AND l.fishing_date BETWEEN vu.usage_start_date AND vu.usage_end_date
JOIN licences li
ON li.vessel_id = vu.Vessel_id
AND li.species = l.species_id
WHERE l.fishing_date = /*date here*/
AND li.licenced_company_id = /*company id here*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.