如何过滤雪花内的一对行

发布于 2025-02-13 21:37:02 字数 1116 浏览 1 评论 0原文

我有一张桌子,就像:

ID宠物国家
45我们
72CA
15CA
36美国
21fr
12
20
1414猪
33iq
90ID

行配对/分组基于匹配宠物。我只想选择包括美国在内的对。基本上,每对国家的价值都将使美国和另一个国家/一个价值之一是美国。例如,结果表将是

ID宠物国家
45US
72CA
15CA
36我们
20我们
14PigRs

这可以通过某种分区来实现吗?我不确定如何在分组中示例选择语句...谢谢!

I have a table A that is like:

IDPETCOUNTRY
45DOGUS
72DOGCA
15CATCA
36CATUS
21SNAKEFR
12SNAKEIN
20PIGUS
14PIGRS
33HORSEIQ
90HORSEID

The rows are paired/grouped based on matching PET. I would like to only select the pairs that include the US in the country. Basically each pair for the country value would have the US and another country/one of the values has to be the US. So for example, the resulting table would be

IDPETCOUNTRY
45DOGUS
72DOGCA
15CATCA
36CATUS
20PIGUS
14PIGRS

Would this be achieved by a partition of some sort? I am not sure how to case the select statement within a grouping... Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与往事干杯 2025-02-20 21:37:02

您可以根据PET加入,首先过滤出country = US

with data_cte (ID,PET,COUNTRY) as (
select * from values 
(45,'DOG','US'),
(72,'DOG','CA'),
(15,'CAT','CA'),
(36,'CAT','US'),
(21,'SNAKE','FR'),
(12,'SNAKE','IN'),
(20,'PIG','US'),
(14,'PIG','RS'),
(33,'HORSE','IQ'),
(90,'HORSE','ID')
), cte_1 as(
select * from data_cte
where country = 'US')
select d.* from data_cte d join
cte_1 c
where d.pet = c.pet;

给出以下结果 -

“在此处输入图像描述”

You can just join based on PET, first filtering out for country = US.

with data_cte (ID,PET,COUNTRY) as (
select * from values 
(45,'DOG','US'),
(72,'DOG','CA'),
(15,'CAT','CA'),
(36,'CAT','US'),
(21,'SNAKE','FR'),
(12,'SNAKE','IN'),
(20,'PIG','US'),
(14,'PIG','RS'),
(33,'HORSE','IQ'),
(90,'HORSE','ID')
), cte_1 as(
select * from data_cte
where country = 'US')
select d.* from data_cte d join
cte_1 c
where d.pet = c.pet;

Gives following result -

enter image description here

笑咖 2025-02-20 21:37:02

使用符合条件和条件聚合:

SELECT *
FROM tab
QUALIFY COUNT_IF(COUNTRY = 'US') OVER(PARTITION BY PET) > 0
   AND COUNT(*) OVER(PARTITION BY PET) > 1;

count_if(country ='us')(pet分区)> 0 - 组中至少必须是我们的

计数(*)(pet)> 1 - 每行PET

样本数据:

CREATE OR REPLACE TABLE tab(ID INT,PET TEXT,COUNTRY TEXT)
AS
SELECT * FROM VALUES (45,'DOG','US'),    (72,'DOG','CA'),
(15,'CAT','CA'),     (36,'CAT','US'),    (21,'SNAKE','FR'),
(12,'SNAKE','IN'),   (20,'PIG','US'),    (14,'PIG','RS'),
(33,'HORSE','IQ'),   (90,'HORSE','ID');

输出:

”在此处输入图像描述”

Using QUALIFY and conditional aggregation:

SELECT *
FROM tab
QUALIFY COUNT_IF(COUNTRY = 'US') OVER(PARTITION BY PET) > 0
   AND COUNT(*) OVER(PARTITION BY PET) > 1;

COUNT_IF(COUNTRY = 'US') OVER(PARTITION BY PET) > 0 - at least one row in the group has to be US

COUNT(*) OVER(PARTITION BY PET) > 1 - more than one row per pet

Sample data:

CREATE OR REPLACE TABLE tab(ID INT,PET TEXT,COUNTRY TEXT)
AS
SELECT * FROM VALUES (45,'DOG','US'),    (72,'DOG','CA'),
(15,'CAT','CA'),     (36,'CAT','US'),    (21,'SNAKE','FR'),
(12,'SNAKE','IN'),   (20,'PIG','US'),    (14,'PIG','RS'),
(33,'HORSE','IQ'),   (90,'HORSE','ID');

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文