0

我正在尝试显示来自 AnnualBudget 的最小值,这是我到目前为止所做的代码:

SELECT WARD.HospitalCode,
       HOSPITAL.HospitalCode Name,
       WardNo,
       AnnualBudget
FROM WARD,
     HOSPITAL
WHERE (AnnualBudget) =
    (SELECT AnnualBudget
     FROM WARD
     WHERE AnnualBudget =
         (SELECT MIN(AnnualBudget)
          FROM WARD
          WHERE AnnualBudget >
              (SELECT MIN(AnnualBudget)
               FROM WARD
          ) 
     )
);

但是,它实际上选择了最大值,而不是选择最小值,有人可以帮忙吗?

4

2 回答 2

0

以下查询将为您提供高于 min 的值

select min(annualbudget)
from ward
where annualbudget > (
  select min(annualbudget)
  from ward
)

如果你想要最小值,试试这个:这些天我没有太多的预言,但它应该可以工作)

select (stuff)

from ward w
join hospital h on h.hospitalId = w.hostpitalId

where w.annualbudget = (select min(annualbudget) from ward)
于 2012-08-04T14:18:38.403 回答
0

这不应该更像这样吗?

SELECT W.HospitalCode, H.HospitalCode, Name, WardNo, AnnualBudget  
FROM WARD W, HOSPITAL H 
WHERE W.HospitalCode = H.HospitalCode
  and W.AnnualBudget = (Select MIN(AnnualBudget) FROM WARD W2 
                                                WHERE W2.HospitalCode = W.HospitalCode)

我假设 WARD 中的医院代码是外键,您希望每个医院的病房预算最低,对吧?

于 2012-08-04T14:23:12.530 回答