我想實現下面的查詢,兩個選擇statments與工會工作不
(SELECT
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
final_avg_total.`DRG Definition`,
final_avg_total.`Provider Id`,
SUM(final_avg_total.avg_total_payments) AS avg_payments,
SUM(final_avg_total.avg_covered_charges) AS avg_covered,
(SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments))/SUM(final_avg_total.avg_covered_charges) AS total_average,
1 - (SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments))/SUM(final_avg_total.avg_covered_charges) AS total_percentage
FROM final_avg_total
GROUP BY final_avg_total.`Provider Id`
ORDER BY total_average DESC LIMIT 0,5)
Union
SELECT
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
final_avg_total.`DRG Definition`,
final_avg_total.`Provider Id`,
SUM(final_avg_total.avg_total_payments) AS avg_payments,
SUM(final_avg_total.avg_covered_charges) AS avg_covered,
(SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments))/SUM(final_avg_total.avg_covered_charges) AS total_average,
1 - (SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments))/SUM(final_avg_total.avg_covered_charges) AS total_percentage
FROM final_avg_total
GROUP BY final_avg_total.`total_percentage`
ORDER BY total_average DESC LIMIT 0,5
實際上這兩個查詢幾乎只用了GROUP BY不同一樣的,但我得到這個錯誤。
5 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION (SELECT
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
fin'
您確定提供者名稱是適合MAX的類型嗎? –
它的類型是文本,但我要求做的,我知道它不是最好的選擇,但BOSS永遠是對的 –
我從查詢中刪除了最大子句,但現在我只得到5個結果我期待10條記錄 –