Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL查询:如何筛选列中的每一组值?_Mysql_Sql_Sql Server - Fatal编程技术网

Mysql SQL查询:如何筛选列中的每一组值?

Mysql SQL查询:如何筛选列中的每一组值?,mysql,sql,sql-server,Mysql,Sql,Sql Server,目前我正在使用SQLFIDLE插入下面的语句 CREATE TABLE table1 ( rn int, pc varchar(25), pc1 varchar(25), grp varchar(25), e_id varchar(25) ); INSERT INTO table1 (rn, pc, pc1, grp, e_id) VALUES(111, 'A1', 'A1', '175', '100'); INSERT INTO table1 (rn, pc, pc1, grp, e_id

目前我正在使用SQLFIDLE插入下面的语句

CREATE TABLE table1
(
rn int,
pc varchar(25),
pc1 varchar(25),
grp varchar(25),
e_id varchar(25)
);

INSERT INTO table1
(rn, pc, pc1, grp, e_id)
VALUES(111, 'A1', 'A1', '175', '100');

INSERT INTO table1
(rn, pc, pc1, grp, e_id)
VALUES(111, 'A1', 'A1', '100', '90');

INSERT INTO table1
(rn, pc, pc1, grp, e_id)
VALUES(112, 'B1', 'B1', '101', '90');

INSERT INTO table1
(rn, pc, pc1, grp, e_id)
VALUES(112, 'B1', 'B1', '100', '90');

INSERT INTO table1
(rn, pc, pc1, grp, e_id)
VALUES(112, 'B1', 'B1', '100', '90');

INSERT INTO table1
(rn, pc, pc1, grp, e_id)
VALUES(113, 'C1', 'C1', '100', '90');

**As you can see there are 2 distinct rn numbers.**

    select *
    from table1 
    where rn in(select rn from table1 group by rn having count(*)>1)
    AND (pc = pc1)
    AND grp in (select max(grp) from table1)
    AND e_id in( select min(e_id) from table1)
我当前的查询能够根据条件找到正确的行

QN:如何使条件适用于每套rn?

因此,结果shld有一行表示111,一行表示112

我试着搜索每一个,但找不到一个好的解决办法

**QN2:我额外添加了一行,其中只有一个rn。我与先前QN的输出应为

RN  PC  PC1 GRP E_ID
111 A1  A1  175 100
112 B1  B1  101 90
113 C1  C1  100 90 --> this row with 1 rn should also output

也许这就是你想要的:

select rn, grp, e_id
from table1 
where rn in (select rn from table1 group by rn having count(*)>1)
AND (pc = pc1)
GROUP BY rn
HAVING grp = max(grp) AND e_id = min(e_id)

但是我不确定你想要什么,因为max GRP和min E_ID是不同的记录,我假设优先级是max GRP,后面是min E_ID

不使用have的查询2

SELECT * from table1
WHERE  pc = pc1
group by rn
order by rn, grp asc, e_id desc
由于'90'>'100',如果您想要“最大的”一个,您需要强制转换为-例如-int:


根据您希望从“重复”行中选择的内容选择聚合函数(如max)。

您想做什么?问题是什么?
按rn分组
从表1中选择distinct(rn)…
?@scrowler我试图通过rn添加grp,我的输出仍然是1 rn请选择一个平台并在使用它们之前查看标记。您使用的是MySQL还是SQL Server?是的,它需要大于1,然后是max grp,然后是min e_id。按此顺序,此查询是否有助于满足您的需要?如果没有,请发布预期的输出。我添加了我的输出,在这种情况下,我应该删除计数,对吗?@ahu-hua,有没有不使用have的方法?@ps4one,是的,将其更改为order by。不管怎样,你为什么不喜欢吃呢?这就像是一个小组在哪里
SELECT * from table1
WHERE  pc = pc1
group by rn
order by rn, grp asc, e_id desc
select rn, max(pc),max(pc1),max(grp),max(e_id) 
from table1 
group by rn;
select rn, max(pc),max(pc1),max(grp),max(cast(e_id as int)) 
from table1 
group by rn;

+------+---------+----------+----------+------------------------+
| rn   | max(pc) | max(pc1) | max(grp) | max(cast(e_id as int)) |
+------+---------+----------+----------+------------------------+
|  111 | A1      | A1       | 175      |                    100 |
|  112 | B1      | B1       | 101      |                     90 |
|  113 | C1      | C1       | 100      |                     90 |
+------+---------+----------+----------+------------------------+