[英]Select distinct row based on highest absolute value
I a MSAccess database which contains the following table 我有一个MSAccess数据库,其中包含下表
Table A 表A
Column1 Column2 Amount 第一栏第二栏金额
======= ======= ====== ======= ======= ======
Value1 Total+ 100 价值1总计+ 100
Value1 Total- -50 值1总计-50
Value2 Total- -233 总计2--233
Value2 Total+ +5 价值2总计+5
I want to be write a query that will give me distinct rows for the highest absolute amounts 我想编写一个查询,该查询将为我提供绝对最高数量的不同行
Expected Results 预期成绩
Column1 Column2 Amount 第一栏第二栏金额
======= ======= ====== ======= ======= ======
Value1 Total+ 100 价值1总计+ 100
Value2 Total- -233 总计2--233
This result set can be retrieved with the following query. 可以使用以下查询来检索此结果集。
Column1 max_abs_value
Value1 100
Value2 233
SELECT
Column1,
Max(Abs(Amount)) AS max_abs_value
FROM Table_A
GROUP BY Column1;
Then, if you want to see the values of Column2
and Amount
for those same rows, you can create a query which uses the first one as a subquery and which you join to Table_A
. 然后,如果要查看这些相同行的
Column2
和Amount
的值,可以创建一个查询,该查询将第一个用作子查询,并加入Table_A
。
SELECT
a.Column1,
a.Column2,
a.Amount
FROM
Table_A AS a
INNER JOIN (
SELECT
Column1,
Max(Abs(Amount)) AS max_abs_value
FROM Table_A
GROUP BY Column1
) AS sub
ON a.Column1 = sub.Column1
WHERE
Abs(Amount)=sub.max_abs_value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.