[英]SQL Window Function to get addresses with more than 1 unique last name present (Snowflake)
I have a Snowflake table which includes addresses, state, first names and last names.我有一个雪花表,其中包括地址、state、名字和姓氏。 I would like to get a query that shows me only the addresses where more than 1 individual with a different last name is present.
我想获得一个查询,仅显示存在超过 1 个具有不同姓氏的个人的地址。
So for example, assume that I have例如,假设我有
address | fname | lname |State
10 lake road| John | Smith |FL
10 lake road| Julie | Gallagher|FL
3 gator cove| Jack | Hoyt |FL
3 gator cove| Debra | Hoyt |FL
I would like the query to return only 1 row in that example: 10 lake road.我希望查询在该示例中仅返回 1 行:10 湖路。 Because it's the only house where there is more than 1 unique last name present.
因为它是唯一一个存在超过 1 个唯一姓氏的房子。
I am currently using我目前正在使用
SELECT distinct a.address, a.fname, a.lname, a.state
FROM clients_addresses a
WHERE a.state = 'FL'
qualify count(1) over( partition by a.lname) > 1
order by a.address
However, this is just returning the addresses where there is more than 1 person, it doesn't care if the last name is repeated.但是,这只是返回超过 1 人的地址,它不关心姓氏是否重复。 That's what I'm trying to avoid.
这就是我试图避免的。
I can't quite understand where the query is going wrong.我不太明白查询出错的地方。 Snowflake doesn't like using any distinct keyword after the initial select, and even if I use it, it only returns 1 occurrence of each address, but it's still just addresses with more than 1 person, even if there was only 1 last name in the address.
Snowflake 不喜欢在最初的 select 之后使用任何不同的关键字,即使我使用它,它也只返回每个地址出现 1 次,但它仍然只是超过 1 人的地址,即使只有 1 个姓氏地址。
It doesn't need to involve the keyword "qualify", I know Snowflake also accepts other things such as subselects that might help with this problem.它不需要涉及关键字“qualify”,我知道 Snowflake 还接受其他可能有助于解决此问题的内容,例如子选择。
I would like the query to return only 1 row in that example: 10 lake road.
我希望查询在该示例中仅返回 1 行:10 湖路。
This sounds like aggregation:这听起来像聚合:
SELECT a.address, count(*)
FROM clients_addresses a
WHERE a.state = 'FL'
GROUP BY a.address
HAVING COUNT(DISTINCT a.lname) > 1;
If you want the original rows (which is not what your question asks for), you can use:如果您想要原始行(这不是您的问题所要求的),您可以使用:
SELECT a.*
FROM clients_addresses a
WHERE a.state = 'FL'
QUALITY COUNT(DISTINCT a.lname) OVER (PARTITION BY a.address) > 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.