所以我得到了這個說法,工作正常:集團通過SQL語句
SELECT MAX(patient_history_date_bio) AS med_date, medication_name
FROM biological
WHERE patient_id = 12)
GROUP BY medication_name
不過,我想有相應的medication_dose也。所以我鍵入這件事
SELECT MAX(patient_history_date_bio) AS med_date, medication_name, medication_dose
FROM biological
WHERE (patient_id = 12)
GROUP BY medication_name
但是,它給了我一個錯誤說:
"coumn 'biological.medication_dose' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.".
所以我儘量將medication_dose到GROUP BY子句中,但隨後它給了我,我不額外行不想要。 我想在我的表格中獲得每種藥物的最新行。 (最新一行由最大功能決定,獲取最新日期)。
我該如何解決這個問題?
它看起來像OP是使用SQL Server。 – 2010-02-28 00:19:46
在c#中使用visual studio 2008編碼的那個。我認爲這是SQL Server的權利? – jello 2010-02-28 00:21:19
另請參閱:http://stackoverflow.com/questions/121387/sql-fetch-the-row-which-has-the-max-value-for-a-column,http://stackoverflow.com/questions/ 95866/select-max-in-group,http://stackoverflow.com/questions/612231/sql-select-rows-with-maxcolumn-value-distinct-by-another-column,和其他許多人。 – outis 2010-02-28 04:44:47