[英]Select Minimum value of column A where Column B=1
為什么不只按top 1
行order by
?
SELECT TOP 1 *
FROM [table]
WHERE EmptyOrNot=1
ORDER BY VhrNum
將TOP 1
與ORDER BY
select Top 1 * from table where EmptyOrNot=1
Order by VhrNum ASC
如果您有多個具有最小VhrNum
值的記錄,並且想要所有平局記錄,則使用TOP 1 WITH TIES
select Top 1 With Ties * from table where EmptyOrNot=1
Order by VhrNum ASC
你可以這樣嘗試
SELECT MIN(VhrNum) FROM table_name where EmptyOrNot=1;
聚合函數需要GROUP BY
或可以ORDER BY
並選擇第一個。
Aggregate不會讓您執行SELECT *
,無論如何這都不是一個好習慣,而Aggregate可以使您更清楚地知道您實際上是在嘗試獲取MIN
。 TOP 1/ORDER BY
可以讓您執行SELECT *
,但是可能並不太清楚您真正想要獲得的只是MIN(VhrNum)
。
骨料:
SELECT MIN(VhrNum)
FROM Average
WHERE EmptyOrNot = 1
GROUP BY EmptyOrNot
最佳:
SELECT TOP(1) VhrNum, *
FROM Average
WHERE EmptyOrNot = 1
ORDER BY VhrNum ASC
您可以嘗試以下方法:
select top 1 * from table where VhrNum= (select min(VhrNum) from table);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.