在Access查询中按不同的列函数选择列

时间:2013-08-26 15:24:07

标签: sql ms-access ms-access-2010

我在Access 2010中有下表。

EQID Breaker Circuit Rating
1    A       One     1000
2    A       Two     1500
3    A       Three   500
4    A       Four    1000
5    B       One     1500
6    B       Two     2000

我想为Group by Breaker创建一个查询,并显示该评级的最低评级和相关电路。我了解如何在不显示最低等级的电路的情况下执行此操作。

我想要的查询结果是:

EQID Breaker Circuit Rating
1    A       Three   500
2    B       One     1500

2 个答案:

答案 0 :(得分:3)

试试这个:

SELECT a.*
FROM table AS a
INNER JOIN (
    SELECT Breaker, MIN(Rating) AS min_rating
    FROM table
    GROUP BY Breaker
) AS b
ON a.Breaker = b.Breaker AND
   a.Rating = b.min_rating;

SQLFiddle:http://www.sqlfiddle.com/#!2/ea4fb/2

答案 1 :(得分:1)

您可以尝试以下内容:

SELECT t.EQID, t.Breaker, t.Circuit, t.Rating 
FROM test t 
INNER JOIN 
(
  SELECT a.Breaker, MIN(a.Rating) AS Rating 
  FROM test a 
  GROUP BY Breaker
 ) AS tmp 
ON tmp.Breaker = t.Breaker AND tmp.Rating = t.Rating;

Sql小提琴演示:http://sqlfiddle.com/#!2/fe796/19