[英]Exclude partial duplicates from query
I need to exclude partial duplicate from my query (MariaDB 5.5.68), for example:我需要从我的查询(MariaDB 5.5.68)中排除部分重复项,例如:
SELECT num, returned FROM table WHERE returned = 1;
num(VARCHAR)![]() |
returned![]() |
---|---|
29.0 ![]() |
1 ![]() |
27.0 ![]() |
1 ![]() |
22.0 ![]() |
1 ![]() |
22.1 ![]() |
1 ![]() |
21.0 ![]() |
1 ![]() |
In this example I need to exclude 22.0
and only keep 22.1
在此示例中,我需要排除
22.0
并仅保留22.1
I have tried to convert the num
row as DOUBLE and call MAX()
but I think I need to do it as a subquery and I cannot get it working.我试图将
num
行转换为 DOUBLE 并调用MAX()
但我认为我需要将其作为子查询来执行,但我无法使其正常工作。
You can use aggregation to achieve this:您可以使用聚合来实现这一点:
SELECT max(cast(num as decimal(4,2))), returned
FROM table
WHERE returned = 1
GROUP BY cast(num as int), returned
In the SELECT clause we cast to decimal for so that max()
will return the numeric max and not the lexicographical max.在 SELECT 子句中,我们将 for 转换为 decimal 以便
max()
将返回数字最大值而不是字典序最大值。 In the GROUP BY
clause we cast to int
to round the number to integer for proper grouping.在
GROUP BY
子句中,我们转换为int
以将数字四舍五入为 integer 以进行正确分组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.