SQL Server – 用另一张表中的相应数据替换逗号分隔的字段

huangapple go评论64阅读模式
英文:

SQL Server - Replace a comma separated field with the appropriate data from another table

问题

以下是翻译好的内容:

我想要 SELECT 人员表,但动态分配适当的爱好。

与其让人们拥有像 'A' 或 'B' 和 'C' 这样的爱好,我想要 SELECT 另一个表格并获取适当的爱好字符串。

一个爱好很简单:

SELECT name, age,
(SELECT hobby
FROM hobbies
WHERE people.hobbies = hobbies.identifier) as hobbies
FROM people

但是,当我尝试想出如何获取逗号分隔字段的多个爱好并替换每个标识符时,我陷入了死胡同。我尝试使用 STRING_SPLITSTRING_AGG,但都没有成功。

欢迎提供任何想法和/或建议!

SQL Server – 用另一张表中的相应数据替换逗号分隔的字段

这是一个包含表定义和示例数据的 fiddle 链接:

https://dbfiddle.uk/fckBUuV-

英文:

I would like to SELECT the people table but dynamically assign the appropriate hobbies.

Instead of people having hobbies like 'A' or 'B' and 'C', I want to SELECT another table and get the appropriate hobby string.

One hobby is easy:

SELECT name, age,
(SELECT hobby
FROM hobbies
WHERE people.hobbies = hobbies.identifier) as hobbies
FROM people

But when I try to think of how to get multiple hobbies for a comma separated field and replace each identifier I reach a dead end. I tried to utilize STRING_SPLIT, STRING_AGG with no luck.

Any thoughts and/or ideas are most welcome!

SQL Server – 用另一张表中的相应数据替换逗号分隔的字段

Here is a fiddle with table definitions and sample data:

> https://dbfiddle.uk/fckBUuV-

答案1

得分: 2

阅读这篇文章也许对你有帮助。下面的查询在SqlServer中有效。

SELECT p.name, p.age, 
    (SELECT STRING_AGG(hobby, ',')
     FROM hobbies h
     WHERE h.identifier IN (SELECT value
                            FROM STRING_SPLIT(p.hobbies, ','))
    ) AS hobbies
FROM people p
英文:

By reading this article maybe this snippet might help you. This query below works in SqlServer.

SELECT p.name, p.age, 
    (SELECT STRING_AGG(hobby, ',')
     FROM hobbies h
     WHERE h.identifier IN (SELECT value
                            FROM STRING_SPLIT(p.hobbies, ','))
    ) AS hobbies
FROM people p

答案2

得分: 2

尝试这个:

SELECT p.Name, p.Age, string_agg(h.Hobby, ',') AS Hobbies
FROM people p
CROSS APPLY string_split(p.Hobbies, ',') ph
INNER JOIN hobbies h ON h.identifier = ph.value
GROUP BY p.Name, p.Age

在这里查看它的工作情况:

> https://dbfiddle.uk/T0aEQyJ4

这是通过在 people.Hobbies 列的 string_split() 上使用 APPLY(侧向连接)来获取每个人/爱好的单独记录来实现的,从那里,简单地连接到 hobbies 表以获取爱好名称,然后按照人的数据进行分组,以便我们可以使用 string_agg() 获取你想要的逗号分隔的列表。

再次强调:这都是不良实践不要在数据库中使用逗号分隔的数据。为这些列表中的每个项目保留单独的记录(通常在它自己的表中),通常最好返回此数据未聚合;让客户端代码或报告工具再次将其汇总。

英文:

Try this:

SELECT p.Name, p.Age, string_agg(h.Hobby, ',') As Hobbies
FROM people p
CROSS APPLY string_split(p.Hobbies, ',') ph
INNER JOIN hobbies h on h.identifier = ph.value
GROUP BY p.Name, p.Age

See it work here:

> https://dbfiddle.uk/T0aEQyJ4

This works by using an APPLY (lateral join) on the string_split() of the people.Hobbies column to get the individual records for each person/hobby you should have had in the first place. From there it's a simple JOIN to the hobbies table to get the hobby names, and then GROUP BY the person data so we can use string_agg() to get the comma-separated list you want.

Again: all of this is poor practice. Don't use comma-separated data in your database. Keep a separate record (usually in it's own table) for each item from such lists, and it's usually better to return this data unaggregated; let the client code or reporting tool roll it up again.

huangapple
  • 本文由 发表于 2023年7月6日 21:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定