2017-08-26 128 views
0

我有以下表中的列和值...MSACCESS 2007 SQL複雜查詢

ColA, ColB, ColC 
b, 90, 1 
p, 95, 5 
p, 100, 6 
p, 99, 6 
p, 98, 6 
b, 94, 5 
b, 93, 1 
b, 92, 3 
o, 89, 3 
b, 88, 4 

我需要以下結果集:

ColA, ColB, ColC 
b, 90, 1 
b, 93, 1 
p, 95, 5 
o, 89, 3 

從本質上講,這是COLC其中最低值ColA是一樣的。所以b的最低值是1,它出現在兩行中。所有p的最小值是5,所有o的最小值是3. ColB是在另一個表上加入的值。所以我確實需要一個查詢來加入ColB上的另一個表。

謝謝。

回答

1

嘗試這個

select mainTable.* from abcTable as mainTable inner join 
(select t.colA,min(t.colC) as minColC from abcTable as t group by t.ColA) as minimumTable 
on mainTable.colA=minimumTable.ColA and mainTable.colC=minimumTable.minColC 
+0

這樣做!謝謝開齋節! – user2184214

+0

Eid - 我不再需要第二個'b'結果行。我只想要ColA分組的行,ColC值最低;如果有領帶,那麼最高的ColB決定領帶。如果他們綁在ColC和ColB上,那麼只返回一行,我不在乎哪個。你能做到嗎? – user2184214

+0

從abcTable中選擇不同的mainTable.colA,max(mainTable.ColB),mainTable.ColC作爲mainTable內部加入 (從tcColA中選擇tCcolA,min(t.colC)作爲minColC作爲t group by t.ColA)作爲minimumTable mainTable.colA上的 = minimumTable.ColA和mainTable.colC = minimumTable.minColC group by mainTable.colA,mainTable.ColC –

0

你在這裏!

with r1 as 
(select * 
,rank() over (partition by ColA order by ColC) fix 
from aba 
) 

select * from r1 where fix = 1 
+0

確定的MS Access suports'RANK()'? – lad2025

+0

我不認爲MSAccess 2007支持這一點。 – user2184214

+0

人們仍然使用MS Access?我認爲這是MS SQL Server。抱歉! – natec