0

我有两张桌子有问题。我正在尝试查找在 2018 年分配了超过 3 名员工的飞行员的姓名。

出租车驾驶台

p_id  p_lname  p_fname  p_city  
043   Smith    John     Dayton
044   Doe      Jane     Cincinnati

分配表

p_id  AT_id   HireNo  AssignDate  City
043   BU78    1       11/29/2018  Dayton
044   BU89    2       11/29/2018  Akron

这只是我所拥有数据的一个简短示例。

我试过以下

select p_fname, p_lname
from taxiPilot tp
join assignment a on tp.p_id = a.p_id
where a.p_id IN (Select count(a.p_id)
from assignment
where AssignDate between "01/01/2018" and "12/31/2018"
group by a.p_id
having count(a.p_id) > 3);

这导致一个空白表

我期待得到以下

p_fname  p_lname  count(a.p_id)
Joe      Smith    5
4

5 回答 5

0

你想做什么?:

    Select count(a.p_id)
from assignment
where AssignDate between "01/01/2018" and "12/31/2018"
group by a.p_id
having count(a.p_id) > 3

这将返回一个数字(因为计数),但不会与分配的 ID 匹配。

于 2019-04-22T01:41:53.150 回答
0

在那种情况下,它会是这样的(我没有测试它):

select p_fname, p_lname, (Select count(a.p_id)
from assignment
where AssignDate between "01/01/2018" and "12/31/2018"
group by a.p_id
having count(a.p_id) > 3)
from taxiPilot tp
join assignment a on tp.p_id = a.p_id
;
于 2019-04-22T01:47:39.313 回答
0

我认为如果您选择日期,简单的聚合将起作用。 小提琴手

select p_fname, p_lname, count(*)
  from taxiPilot tp 

join assignment a 
  on tp.p_id = a.p_id

where a.AssignDate between CAST('2018-01-01' AS DATETIME) 
                       and CAST('2018-12-31' AS DATETIME)
group by p_fname, p_lname
having count(*) > 3
于 2019-04-22T01:58:38.987 回答
0
with paste as (
select 
p_id, count(a.p_id) 
from assignment
group by 1
having count(*) > 3
)

select p_lname,  p_fname, max(HireNo)
from taxiPilot 
where p_id in (select p_id from paste)
group by 1, 2

不过没试过。

于 2019-04-22T02:03:45.697 回答
0

首先,您的想法是正确的,但您的子查询不只与一个飞行员相关。你只是抓住了日期之间的所有任务,但与哪个飞行员没有相关性。所以你可以在所有飞行员中计算出 273 人。

相反,您可以在子查询中添加 Pilot id,然后在此之外加入。这样,您就不会试图拉动所有飞行员。得到那些合格的,然后得到名字。

select
      tp.p_fname,
      tp.p_lname,
      PQ.NumHires
   from
      ( select a.p_id, count(*) NumHires
           from Assignment a
           where a.AssignDate between '2018-01-01' and '2018-12-31'
           group by a.p_id 
           having count(*) > 3) PQ
         JOIN taxiPilot tp
            on PQ.p_id = tp.p_id
   order by
      tp.p_lname,
      tp.p_fname
于 2019-04-22T02:18:25.867 回答