简体   繁体   English

如何在sql中以固定日期间隔获取记录

[英]How to get records with in fixed date interval in sql

The federal aviation authority wants to know how many types of aircraft each of your pilots (employee only ie EMP) is currently certified (qualifies) to fly.联邦航空当局想知道您的每个飞行员(仅限员工,即 EMP)目前获得认证(有资格)飞行的飞机类型。 Assume that the validity of a certification is only for one year .假设认证的有效期只有一年 So, list the pilots and the number of types of aircraft each is currently qualified to fly (date_certified).因此,请列出飞行员和每个目前有资格飞行的飞机类型的数量(date_certified)。 Please list pilot full name, license number, and number of types s/he is currently qualified (certified) to fly.请列出飞行员的全名、执照号码和他/她目前有资格(认证)飞行的类型数量。 (hint: SYSDATE returns today's date like date() in MS Access. (SYSDATE +30) is 30 days from today). (提示:SYSDATE 返回今天的日期,如 MS Access 中的 date()。(SYSDATE +30) 是从今天算起 30 天)。

HERE IS THE PILOT TABLE:这是飞行员表:

pilot_nbr, license_nbr, last_name, first_name, title, address, phone, office_nbr, contract_type, salary, manager_nbr  

'701', '7111', 'Dark', 'Jack', '1st officer', '6 street', '6042233445', '789', 'PTE', '145000.00', NULL
'702', '7222', 'Mack', 'Bill', '1st officer', '7 street', '6043344556', '890', 'EMP', '155000.00', '701'
'703', '7333', 'Cheung', 'Charles', '2nd officer', '8 street', '6044455667', '503', 'PTE', '140000.00', '701'
'704', '7444', 'Gordon', 'Greg', '1st officer', '9 street', '6045566778', '123', 'EMP', '125000.00', '701'
'705', '7555', 'Basso', 'Nicki', '2nd officer', '5 street', '6046677889', '223', 'EMP', '163000.00', '701'
'706', '7666', 'Vettel', 'Sebast', '1st officer', '5 street', '6046677800', '523', 'EMP', '199000.00', '701'
'707', '7777', 'Hawke', 'Mike', '2nd officer', '7 street', '6046677326', '423', 'EMP', '139000.00', '701'  

HERE IS THE QUALIFIES TABLE:这是资格表:

pilot_nbr, plane_type, date_certified  

'701', 'DE Prop', '2013-08-14'
'701', 'SE Prop', '2013-04-10'
'702', 'DE Prop', '2013-02-22'
'702', 'SE Jet', '2013-04-10'
'702', 'SE Prop', '2013-06-21'
'703', 'DE Prop', '2013-04-10'
'703', 'SE Jet', '2013-04-10'
'703', 'SE Prop', '2013-04-10'
'704', 'DE Prop', '2013-06-12'
'704', 'ME Jet', '2013-08-21'
'704', 'SE Jet', '2013-06-15'
'704', 'SE Prop', '2013-04-10'  

HERE IS WHAT I TRIED (DON'T KNOW IF IT IS CORRECT)这是我试过的(不知道它是否正确)

SELECT first_name, last_name, license_nbr, Count(first_name) As 'No of types of airplane certified to fly'
FROM pilot, qualifies 
WHERE pilot.pilot_nbr=qualifies.pilot_nbr AND contract_type='EMP' AND date_certified >=  DATE_SUB(NOW(), INTERVAL 1 YEAR)
GROUP BY first_name, last_name  

Your help will be appreciated.您的帮助将不胜感激。

Hope this will solve your task...

SELECT p.first_name, p.last_name, p.license_nbr, Count(q.plane_type) As 'No of types of airplane certified to fly'
FROM pilot p inner join qualifies q on p.pilot_nbr = q.pilot_nbr
WHERE p.contract_type='EMP' AND DATE_SUB(q.date_certified, INTERVAL 1 YEAR) > CURDATE()
GROUP BY p.first_name, p.last_name, p.license_nbr;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM