非常感谢任何帮助。
我有一家餐 table 医院:
Nurse + Year + No.Patients<br/>
A001 |2000 | 23 <br/>
A001 |2001 | 30 <br/>
A001 |2002 | 35 <br/>
<br/>
B001 |2000 | 12 <br/>
B001 |2001 | 15 <br/>
B001 |2002 | 45 <br/>
<br/>
C001 |2000 | 50 <br/>
C002 |2001 | 59 <br/>
C003 |2002 | 69 <br/>
etc
<p>What I am trying to do is work out which nurse
had the greatest increase of patients for the years 2000 - 2002.</p>
<p>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 <code>B001 | 33</code>.</p>
<p>This is what I have so far:</p>
<pre><code>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;
</code></pre>
<p>but all I get returned is an empty set.</p>
I think I need an overall max(nopats)
加入后。
这里的任何帮助都会很棒。
谢谢!
最佳答案
试试这个:
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 |由于 LIMIT 1,为 33;如果您想要更多结果,请将其保留。
关于mysql - 查询返回空集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1148930/