选择最长独特字符串的行

发布于 2025-02-05 18:44:20 字数 2425 浏览 2 评论 0原文

我试图弄清楚如何从表中选择一个行的子集,每个“父”字符串最长的唯一字符串。我将在下面提供表示例,而我的代码无效。

Current Table:

NameSalePrice
NorthAmerica\US\Northeast\NewYork8576
NorthAmerica\US\Northeast2598
SouthAmerica\Brazil1348
SouthAmerica\Chile\NorthEast9726
SouthAmerica\Chile4412
NorthAmerica\Canada\Ontario3894
NorthAmerica\Canada6321

Desired Output:

NameSalePrice
NorthAmerica\美国\东北\ Newyork8576
Southamerica \巴西1348
Southamerica \ Chile \ Northeast9726
Northamerica \ Canada \ Canada \ Ontario3894

最初,我认为我可以基于Backslashes()(

example: SELECT * FROM TestTable WHERE Name LIKE '%\\%'

)( )()()。但是,此逻辑不起作用,因为某些最远的分支可能只有1个后斜线,而其他分支可能有3个以上。

生成测试表的代码如下,任何帮助/建议都将不胜感激

create table t1( 
[name] varchar(60), 
[saleprice] int  );


insert into t1 values ('NorthAmerica\US\Northeast\NewYork',8576);
insert into t1 values ('NorthAmerica\US\Northeast',2598);
insert into t1 values ('SouthAmerica\Brazil',1348);
insert into t1 values ('SouthAmerica\Chile\NorthEast',9726);
insert into t1 values ('SouthAmerica\Chile',4412);
insert into t1 values ('NorthAmerica\Canada\Ontario',3894);
insert into t1 values ('NorthAmerica\Canada',6321);

I'm trying to figure out how to select a subset of rows from a table, with the longest unique string for each "parent" string. I'll provide table examples below and my code that didn't work.

Current Table:

NameSalePrice
NorthAmerica\US\Northeast\NewYork8576
NorthAmerica\US\Northeast2598
SouthAmerica\Brazil1348
SouthAmerica\Chile\NorthEast9726
SouthAmerica\Chile4412
NorthAmerica\Canada\Ontario3894
NorthAmerica\Canada6321

Desired Output:

NameSalePrice
NorthAmerica\US\Northeast\NewYork8576
SouthAmerica\Brazil1348
SouthAmerica\Chile\NorthEast9726
NorthAmerica\Canada\Ontario3894

Originally, I thought I could apply some form of logic based off the number of backslashes (

example: SELECT * FROM TestTable WHERE Name LIKE '%\\%'

). However, this logic doesn't work because some names furthest branch may only have 1 backslash while others may have 3+.

Code to generate test table is below and any help/advice would be greatly appreciated

create table t1( 
[name] varchar(60), 
[saleprice] int  );


insert into t1 values ('NorthAmerica\US\Northeast\NewYork',8576);
insert into t1 values ('NorthAmerica\US\Northeast',2598);
insert into t1 values ('SouthAmerica\Brazil',1348);
insert into t1 values ('SouthAmerica\Chile\NorthEast',9726);
insert into t1 values ('SouthAmerica\Chile',4412);
insert into t1 values ('NorthAmerica\Canada\Ontario',3894);
insert into t1 values ('NorthAmerica\Canada',6321);

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

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

发布评论

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

评论(1

独﹏钓一江月 2025-02-12 18:44:20

使用操作员喜欢不出现

SELECT t1.*
FROM t1
WHERE NOT EXISTS (
  SELECT 1
  FROM t1 AS t2
  WHERE t2.name LIKE t1.name + '_%'
);

请参阅演示

Use the operator LIKE with NOT EXISTS:

SELECT t1.*
FROM t1
WHERE NOT EXISTS (
  SELECT 1
  FROM t1 AS t2
  WHERE t2.name LIKE t1.name + '_%'
);

See the demo.

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