2013-10-29 50 views
0

我有一個表,如下SQL獲取行時與MIN(COL A)如果2行存在,那麼MIN(COLB)

Table1 
(
col A float, 
col B float 
) 

I need a row with min(col A). If 2 rows have same col A value then 
row with min(col B). 

EX: (case 1) 

    A  B 
    1.2 1.5 
    1.5 1.0 
Result should show be first row 

(case 2) 

    A  B 
    1.2 1.5 
    1.2 1.0 
Result should show be second row 

Is it possible in a single query? Please help

回答

1

似乎是一個最簡單的方法:

select top 1 cola, colb 
from table1 
order by cola, colb 
+0

感謝它解決了我的問題。如果你有col A和col B是NULLABLE,是否有任何問題? – Manju

0
select top 1 * from [dbo].[12] where a = (select min(a) from [dbo].[12]) OR b=(select min(b) from [dbo].[12]) 
+0

如果min(b)與min(a)不在同一行,該怎麼辦? :) –

+0

但它適用於這個前?下面的答案與此相同? –

+0

'OR'也不能解決它,因爲這將返回任何記錄與最小(a)或分(b) –