[英]Filter by Last AND First Name in SQL
I have a table with last and first names in separate columns.我有一个表,其中姓氏和名字分别位于不同的列中。 I need to return all rows containing duplicate last and first names.
我需要返回包含重复姓氏和名字的所有行。 I have this so far and it works to filter by last name only but not sure how to filter by the first name too.
到目前为止,我有这个,它只能按姓氏过滤,但不知道如何也按名字过滤。 Would appreciate some guidance.
希望得到一些指导。 Thanks!
谢谢!
with cte1 as (
select
distinct lastName as last_name,
count(lastName) over (partition by lastName) as ln_count
from peoplelist
),
cte2 as (
select
ng.*
from
peoplelist ng
left join
cte1 on cte1.last_name = ng.LastName
where cte1.ln_count > 1
order by LastName desc
)
select * from cte2
You could concat the names together and just perform a count by full name and add the full name with a group by and having > 1您可以将名称连接在一起,只需按全名进行计数,然后将全名与组添加并具有 > 1
SELECT
CONCAT(FIRSTNAME, ' ', LastName) AS FULLNAME
, COUNT(CONCAT(FIRSTNAME, ' ', LastName)) AS FULLNAMECOUNT
FROM
PEOPLELIST
GROUP BY CONCAT(FIRSTNAME, ' ', LastName)
HAVING COUNT(CONCAT(FIRSTNAME, ' ', LastName)) > 1
select distinct(CONCAT(FirstName , ' ', LastName)) from names
where
FirstName not in (select FirstName from names group by FirstName having count(*) = 1)
and
LastName not in (select LastName from names group by LastName having count(*) = 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.