2016-08-03 75 views
-1

嗨,大家好,我真的有問題。從分組列中選擇最大值的Sql問題

select * from Insurance_Plan; 
    select * from Enroll; 
    select * from Patient; 

這三個是我的數據庫中的表。現在我必須爲患者最多的參與者獲得保險計劃,併爲此計劃,名​​稱,所需的共付金額以及患者登記人數。

我已經做了部分到這裏:

Select I.Insurance_Name,Max(I.Insurance_Copayment_Amount) As 
    Copayment_Amount,count(P.Patient_ID) As Number_Of_Patients 
    From Insurance_Plan as I inner Join 
    Enroll AS E on I.Insurance_Plan_ID=E.Insurance_Plan_ID 
    inner join Patient as P on P.Patient_ID=E.Patient_ID 
    group by Insurance_Name 

其結果是我得到的輸出: enter image description here

現在我想只顯示與入選的患者最大數量的保險計劃,是顯示的輸出中的第二行。我正在嘗試使用Max函數,但遇到錯誤。它必須在一個有效的查詢中完成。

+0

我從這個問題中刪除了'mysql'標記 - 該屏幕截圖必須來自'ssms'。隨意更新,如果我不正確,只是不標記兩個數據庫... – sgeddes

回答

0
Select top (1) I.Insurance_Name,Max(I.Insurance_Copayment_Amount) As 
    Copayment_Amount,count(P.Patient_ID) As Number_Of_Patients 
    From Insurance_Plan as I inner Join 
    Enroll AS E on I.Insurance_Plan_ID=E.Insurance_Plan_ID 
    inner join Patient as P on P.Patient_ID=E.Patient_ID 
    group by Insurance_Name 
    order by count(P.Patient_ID) desc 
+0

嘿謝謝...但通過添加3按子句順序我們將其從動態更改爲手動完成。所以任何其他方式 – Kam

+0

請看看編輯 – niketshah90