gpt4 book ai didi

SQL:在单个字段中包含多个值的嵌套 SELECT

转载 作者:行者123 更新时间:2023-12-03 02:05:54 26 4
gpt4 key购买 nike

在我的 SQL 2005 数据库中,我有一个表,其中的值存储为 ID,并与其他表有关系。因此,在我的 MyDBO.warranty 表中,我存储了 Product_id 而不是 Product_name 以节省空间。产品名称存储在 MyDBO.products 中。

当营销部门提取人口统计信息时,查询会从相关表中为每个 ID 选择相应的名称(为简洁起见,进行了精简):

SELECT w1.warranty_id AS "No.",
w1.created AS "Register Date"
w1.full_name AS "Name",
w1.purchase_date AS "Purchased",
(
SELECT p1.product_name
FROM WarrDBO.products p1 WITH(NOLOCK)
WHERE p1.product_id = i1.product_id
) AS "Product Purchased",
i1.accessories
FROM WarrDBO.warranty w1
LEFT OUTER JOIN WarrDBO.warranty_info i1
ON i1.warranty_id = w1.warranty_id
ORDER BY w1.warranty_id ASC

现在,我的问题是 Warranty_info 表中的“accessories”列存储了多个值:

No.     Register Date    Name             Purchased       Accessories
---------------------------------------------------------------------
1500 1/1/2008 Smith, John Some Product 5,7,9
1501 1/1/2008 Hancock, John Another 2,3
1502 1/1/2008 Brown, James And Another 2,9

我需要对“Accessories”执行与“Product”类似的操作,并从 MyDBO.accessories 表中提取 accessory_name使用accessory_id。我不知道从哪里开始,因为首先我需要提取 ID,然后以某种方式将多个值连接成一个字符串。所以每一行都会有“accessoryname1,accessoryname2,accessoryname3”:

No.     Register Date    Name             Purchased       Accessories
---------------------------------------------------------------------
1500 1/1/2008 Smith, John Some Product Case,Bag,Padding
1501 1/1/2008 Hancock, John Another Wrap,Label
1502 1/1/2008 Brown, James And Another Wrap,Padding

我该怎么做?

编辑>>发布我的最终代码:

我创建了这个函数:

CREATE FUNCTION SQL_GTOInc.Split
(
@delimited varchar(50),
@delimiter varchar(1)
) RETURNS @t TABLE
(
-- Id column can be commented out, not required for sql splitting string
id INT identity(1,1), -- I use this column for numbering splitted parts
val INT
)
AS
BEGIN
declare @xml xml
set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'

insert into @t(val)
select
r.value('.','varchar(5)') as item
from @xml.nodes('//root/r') as records(r)

RETURN
END

并相应地更新了我的代码:

SELECT w1.warranty_id, 
i1.accessories,
(
CASE
WHEN i1.accessories <> '' AND i1.accessories <> 'NULL' AND LEN(i1.accessories) > 0 THEN
STUFF(
(
SELECT ', ' + a1.accessory
FROM MyDBO.accessories a1
INNER JOIN MyDBO.Split(i1.accessories, ',') a2
ON a1.accessory_id = a2.val
FOR XML PATH('')
), 1, 1, ''
)
ELSE ''
END
) AS "Accessories"
FROM MyDBO.warranty w1
LEFT OUTER JOIN MyDBO.warranty_info i1
ON i1.warranty_id = w1.warranty_id

最佳答案

您可以编写一个表值函数,该函数只需将逗号分隔的字符串拆分为 XML 并将 XML 节点转换为行。

参见: http://www.kodyaz.com/articles//t-sql-convert-split-delimeted-string-as-rows-using-xml.aspx

通过函数调用的结果连接到附件,并将结果填充回逗号分隔的名称列表。

未经测试的代码:

SELECT w1.warranty_id AS "No.",
w1.created AS "Register Date"
w1.full_name AS "Name",
w1.purchase_date AS "Purchased",
(
SELECT p1.product_name
FROM WarrDBO.products p1 WITH(NOLOCK)
WHERE p1.product_id = i1.product_id
) AS "Product Purchased",
STUFF(
(
SELECT
', ' + a.name
FROM [table-valued-function](i1.accessories) acc_list
INNER JOIN accessories a ON acc_list.id = a.id
FOR XML PATH('')
), 1, 1, ''
) AS [accessories]
FROM WarrDBO.warranty w1
LEFT OUTER JOIN WarrDBO.warranty_info i1
ON i1.warranty_id = w1.warranty_id
ORDER BY w1.warranty_id ASC

关于SQL:在单个字段中包含多个值的嵌套 SELECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5518612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com