简体   繁体   English

获取非重复记录的最大值

[英]get max value of non duplicated records

Is it possible this?这可能吗? I have the following table:我有下表:

Col1列1 Col2列2 col3列3
code1代码1 US我们 9 9
code1代码1 0 0 5 5个
code2代码2 US我们 4 4个
code2代码2 0 0 11 11
code3代码3 0 0 11 11

and I'm trying to get the higher col3 value filtering by col1 and col2 my attempt right now is:我正在尝试通过 col1 和 col2 过滤更高的 col3 值我现在的尝试是:

SELECT MAX(col3), col2, col1, count(col1) FROM `mytable` WHERE (col1 IN ('code1', 'code2') ) AND ((col2 = 'US') OR (col2 = '0')) GROUP BY col1;

and my result:和我的结果:

Col1列1 Col2列2 col3列3 count(col1)计数(col1)
code1代码1 US我们 9 9 2 2个
code2代码2 0 0 11 11 2 2个

But what I need is if both codes have a col2 with value equal to 'US' then return the higher col3 value from those and ignore the ones with '0'但我需要的是,如果两个代码都有一个值等于“US”的 col2,则从这些代码中返回较高的 col3 值,并忽略带有“0”的代码

Col1列1 Col2列2 col3列3 count(col1)计数(col1)
code1代码1 US我们 9 9 2 2个

or if one of those codes only have one row with col2 equal to '0' then compare between the code1 = 'US' and the code2 = '0'或者,如果其中一个代码只有一行,col2 等于“0”,则比较代码 1 =“美国”和代码 2 =“0”

Col1列1 Col2列2 col3列3
code1代码1 US我们 9 9
code1代码1 0 0 5 5个
code2代码2 0 0 11 11
code3代码3 0 0 11 11

giving the result:给出结果:

Col1列1 Col2列2 col3列3 count(col1)计数(col1)
code2代码2 0 0 11 11 2 2个

Is it possible to achieve this with a mysql query?是否可以通过 mysql 查询来实现? or should I use php?还是我应该使用 php?

Thank you in advance.先感谢您。

You have very specific where clause, so you need to repeat them in teh subselect as in the Main Select您有非常具体的 where 子句,因此您需要像在 Main Select 中那样在子选择中重复它们

As your needs get more and more omplicated, you must increase the number of subselect to meet your requereiments, the ORDER BYs determine the row that is selected.随着您的需求变得越来越复杂,您必须增加子选择的数量以满足您的要求,ORDER BY 确定被选中的行。

Followimg gets you your result, as you add more and more obsticles you must do it also in the code Followimg 为您提供结果,随着您添加越来越多的障碍物,您也必须在代码中执行此操作

CREATE TABLE tab1 ( `Col1` VARCHAR(5), `Col2` VARCHAR(2), `col3` INTEGER ); INSERT INTO tab1 (`Col1`, `Col2`, `col3`) VALUES ('code1', 'US', '9'), ('code1', '0', '5'), ('code2', 'US', '4'), ('code2', '0', '11'), ('code3', '0', '11');
 SELECT * FROM tab1 WHERE col3 = (SELECT MAX(col3) FROM tab1 WHERE (col1 IN ('code1', 'code2') ) AND ((col2 = 'US') OR (col2 = '0'))) AND (col1 IN ('code1', 'code2') ) AND ((col2 = 'US') OR (col2 = '0')) ORDER BY col1,col2 DESC LIMIT 1
 Col1 |列 1 | Col2 |列2 | col3:---- |:--- | col3:---- |:--- | ---: code2 | ---:代码2 | 0 | 0 | 11 11
 SELECT col1,col2,col3 FROM (SELECT `Col1`, `Col2`, `col3`, ROW_NUMBER() OVER(ORDER BY col2 DESC,col3 DESC) rn2 FROM( SELECT *,ROW_NUMBER() OVER(PARTITION BY col1 ORDER BY col3 DESC) rn FROM tab1) t1 WHERE rn = 1) t2 WHERE rn2 = 1
 col1 | col1 | col2 | col2 | col3:---- |:--- | col3:---- |:--- | ---: code1 | ---:代码1 | US |美国 | 9 9
 CREATE TABLE tab2 ( `Col1` VARCHAR(5), `Col2` VARCHAR(2), `col3` INTEGER ); INSERT INTO tab2 (`Col1`, `Col2`, `col3`) VALUES ('code1', '0', '5'), ('code2', 'US', '4'), ('code2', '0', '11'), ('code3', '0', '11');
 SELECT col1,col2,col3 FROM (SELECT `Col1`, `Col2`, `col3`, ROW_NUMBER() OVER(ORDER BY col2 DESC,col3 DESC) rn2 FROM( SELECT *,ROW_NUMBER() OVER(PARTITION BY col1 ORDER BY col3 DESC) rn FROM tab2) t1 WHERE rn = 1) t2 WHERE rn2 = 1
 col1 | col1 | col2 | col2 | col3:---- |:--- | col3:---- |:--- | ---: code2 | ---:代码2 | 0 | 0 | 11 11

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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