当我使用手动分组仅按 1 个字段过滤数据时,如何包含表的所有其他字段?

How can I include all the other fields of table when I'm filtering the data by only 1 field using group by hand having?

提问人:Da Chen 提问时间:11/9/2023 最后编辑:PM 77-1Da Chen 更新时间:11/9/2023 访问量:33

问:

所以这是我下面的查询:

select creditcard
from  user where preferred_card = '1'
and uder_identification_code = 'creditcard' 
group by creditcard having count (1) > 1;

因此,我正在尝试获取所有拥有 1 张以上信用卡作为首选卡的用户的列表(基本上这是一次数据清理,因为应该只有 1 张首选)。

但是由于我正在进行分组依据并具有计数,因此当我尝试放入其他字段时,查询不会运行,因为逻辑错误。如何使查询显示用户表中的其他字段,例如 lastname、dob?我正在尝试提取所有用户列表,以便进行数据清理。

以下所有更改均失败:

select creditcard, lastname
from  user where preferred_card = '1'
and uder_identification_code = 'creditcard' 
group by creditcard having count (1) > 1 and lastname;`



select *
from  user where preferred_card = '1'
and uder_identification_code = 'creditcard' 
group by creditcard having count (1) > 1 and lastname;

我试过按同一表中的多个字段分组

SQL 筛选器 group-by 计数 具有

评论

0赞 Isolated 11/9/2023
将姓氏移至分组依据?
0赞 Ivan Yuzafatau 11/9/2023
您应该将其他字段添加到 GROUP BY。

答:

0赞 Cetin Basoz 11/9/2023 #1

您可以使用从组中获取的数据查询表,如下所示:

select * from user
where creditcard in 
(select creditcard 
from  user 
where preferred_card = '1' and uder_identification_code = 'creditcard'  
group by creditcard having count (1) > 1);