从查询返回的空集

时间:2009-07-19 00:10:19

标签: mysql

非常感谢任何帮助。

我有一家餐桌医院:

加入后

Nurse + Year + No.Patients
A001 |2000 | 23
A001 |2001 | 30
A001 |2002 | 35

B001 |2000 | 12
B001 |2001 | 15
B001 |2002 | 45

C001 |2000 | 50
C002 |2001 | 59
C003 |2002 | 69
etc

What I am trying to do is work out which nurse had the greatest increase of patients for the years 2000 - 2002.

Clearly B001 did as her patients increased from 12 to 45 and increase of 33 and what I am trying to produce is the result B001 | 33.

This is what I have so far:

select a.nurse,a.nopats from hospital as a
  join
    ( select nurse,max(nopats)-min(nopats) as growth
        from hospital where year between 2000 and 2002 group by nurse ) as s1
  on a.nurse = s1.nurse and a.nopats = s1.growth
  where year between 2000 and 2002;

but all I get returned is an empty set.

B001 | 33

这里的任何帮助都会很棒。

谢谢!

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT nurse, (max(nopats) - min(nopats)) AS growth
  FROM hospital
 WHERE year BETWEEN 2000 AND 2002
 GROUP BY nurse
 ORDER BY growth DESC
LIMIT 1;

结果:B001 | 33由于LIMIT 1;如果你想要更多的结果,就把它留下吧。

答案 1 :(得分:1)

SELECT nurse, MAX(nopats) - MIN(nopats) AS Growth
  FROM hospital 
 WHERE year BETWEEN 2000 AND 2002
 GROUP BY nurse
 ORDER BY Growth

应该这样做。如果那就是你需要的,请告诉我。