Qais Al-Rawahneh Qais Al-Rawahneh - 12 days ago 7
SQL Question

Select Minimum value of column A where Column B=1

I have this table AVERAGE

enter image description here

I want to

select * from table where VhrNum=MIN(VhrNum) and EmptyOrNot=1


I've Tried this query but it's not working

select *
from Average
where Itmnum='1'
and VhrNum = (
select MIN(VhrNum)
from (
select *
from Average
where EmptyOrNot = '1'
)
)


In my case it's should select the third row

Answer

Why not just take the top 1 row and order by by the column?

SELECT TOP 1 * 
FROM [table]
WHERE EmptyOrNot=1
ORDER BY VhrNum
Comments