简体   繁体   English

从查询中排除部分重复项

[英]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 29.0 1 1个
27.0 27.0 1 1个
22.0 22.0 1 1个
22.1 22.1 1 1个
21.0 21.0 1 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM