2012-08-04 73 views
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 
     ) 
    ) 
); 

但而不是選擇最小值,它實際上選擇最大價值,任何人都可以幫忙嗎?

+0

您的數字是否爲偶然的負值? – 2012-08-04 14:13:36

+0

表中的值都是正值,我真的很困惑,爲什麼會發生這種情況。 – jeremy 2012-08-04 14:18:28

+0

這是一個非常可怕的嵌套子查詢集。當然有更好的方法來重寫它們。 – Vic 2012-08-04 14:20:38

回答

0

下面的查詢會給你的東西比分鐘時

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) 
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中的醫院代碼是一個外鍵,並且您希望每個醫院的預算最低,對吧?

+0

我只是想通過使用子查詢來獲得最低預算的WARD,我嘗試了你寫的內容,並且顯示了最低和次低。 – jeremy 2012-08-04 14:26:40

+0

我自己排序,感謝您的幫助!非常驚訝。 – jeremy 2012-08-04 14:31:13

+0

你最終得到了什麼? – hol 2012-08-04 14:32:28