I have a table in which I need to return a distinct value for each id based on the highest reference value. As an example, this table:
我有一个表,我需要根据最高参考值为每个id返回一个不同的值。作为一个例子,这个表:
my table
|--------|----------|-------------|
| ID | Amount | Ref_value |
|---------------------------------|
| 1 | 200 | 5 |
| 1 | 120 | 8 |
| 2 | 150 | 3 |
| 3 | 200 | 4 |
|--------|----------|-------------|
I need to get one return per ID, but since ID=1 appears more than once, I need to select the one with the highest "Ref_value". So my result would be:
我需要为每个ID获得一次返回,但由于ID = 1出现不止一次,我需要选择具有最高“Ref_value”的那个。所以我的结果是:
result of the query over my table
|--------|----------|-------------|
| ID | Amount | Ref_value |
|---------------------------------|
| 1 | 120 | 8 |
| 2 | 150 | 3 |
| 3 | 200 | 4 |
|--------|----------|-------------|
3 个解决方案
#1
1
you could use a inner join on the max value grouped by id
您可以在按ID分组的最大值上使用内部联接
select * from my_table
inner join (
select id, max(ref_value) max_value
from my_table
group by id
) t on t.id = my_table_id, t.max_vale = my_table_value
#2
1
If your database supports window functions, this solution will access the table only once:
如果您的数据库支持窗口函数,则此解决方案仅访问该表一次:
SELECT id, amount, ref_value
FROM (
SELECT t.*, row_number() OVER (PARTITION BY id ORDER BY ref_value DESC) rn
FROM t
) t
WHERE rn = 1
If you're using Oracle, you might use this solution that I've found to be a bit faster in most cases:
如果你正在使用Oracle,你可能会使用我发现在大多数情况下更快一点的解决方案:
SELECT
max(id) KEEP (DENSE_RANK FIRST ORDER BY ref_value DESC) id,
max(amount) KEEP (DENSE_RANK FIRST ORDER BY ref_value DESC) amount,
max(ref_value) KEEP (DENSE_RANK FIRST ORDER BY ref_value DESC) ref_value,
FROM t
GROUP BY id
Both of the above solutions will return an arbitrary row if two ref_value
values are tied.
如果两个ref_value值相关联,则上述两种解决方案都将返回任意行。
If none of the above apply, scaisEdge's solution works on all databases.
如果以上都不适用,scaisEdge的解决方案适用于所有数据库。
#3
1
Select * might throw an ambiguous column names error. So you might want to select the only required columns there.
选择*可能会抛出一个模糊的列名错误。因此,您可能希望在那里选择唯一需要的列。
SELECT A.ID, A.AMOUNT, A.REF_VALUE
FROM
MYTABLE A
INNER JOIN
(SELECT ID, MAX(REF_VALUE) AS MAX_REF FROM MYTABLE GROUP BY ID) B
ON A.ID = B.ID AND A.REF_VALUE = B.MAX_REF;
#1
1
you could use a inner join on the max value grouped by id
您可以在按ID分组的最大值上使用内部联接
select * from my_table
inner join (
select id, max(ref_value) max_value
from my_table
group by id
) t on t.id = my_table_id, t.max_vale = my_table_value
#2
1
If your database supports window functions, this solution will access the table only once:
如果您的数据库支持窗口函数,则此解决方案仅访问该表一次:
SELECT id, amount, ref_value
FROM (
SELECT t.*, row_number() OVER (PARTITION BY id ORDER BY ref_value DESC) rn
FROM t
) t
WHERE rn = 1
If you're using Oracle, you might use this solution that I've found to be a bit faster in most cases:
如果你正在使用Oracle,你可能会使用我发现在大多数情况下更快一点的解决方案:
SELECT
max(id) KEEP (DENSE_RANK FIRST ORDER BY ref_value DESC) id,
max(amount) KEEP (DENSE_RANK FIRST ORDER BY ref_value DESC) amount,
max(ref_value) KEEP (DENSE_RANK FIRST ORDER BY ref_value DESC) ref_value,
FROM t
GROUP BY id
Both of the above solutions will return an arbitrary row if two ref_value
values are tied.
如果两个ref_value值相关联,则上述两种解决方案都将返回任意行。
If none of the above apply, scaisEdge's solution works on all databases.
如果以上都不适用,scaisEdge的解决方案适用于所有数据库。
#3
1
Select * might throw an ambiguous column names error. So you might want to select the only required columns there.
选择*可能会抛出一个模糊的列名错误。因此,您可能希望在那里选择唯一需要的列。
SELECT A.ID, A.AMOUNT, A.REF_VALUE
FROM
MYTABLE A
INNER JOIN
(SELECT ID, MAX(REF_VALUE) AS MAX_REF FROM MYTABLE GROUP BY ID) B
ON A.ID = B.ID AND A.REF_VALUE = B.MAX_REF;