我遇到了一些给我的代码有问题。它不是应该连接的行。我从3个表中拉出来选择要连接的行。我有一个Comment
,WorkOrderT
和WorkMaterial_nonFiltered
表。
Comment
表显然有注释,并且链接到WorkMaterial_nonFiltered
表。 WorkMaterial_nonFiltered
表连接到工作订单ID所在的WorkOrderT
表。
我正在尝试使用工作订单ID来获取该工单的所有注释,并将它们全部连接到每个工单ID的一行中。这是我目前拥有的代码:
select
wo.WOId as ID,
STUFF(cast((select distinct '; ' + c.comments
from working.Comment c
where c.Instance_InstanceId = wm.id
for xml path(''), type) as varchar(max)), 1, 1, ' ') as MaterialComments
from
working.WorkOrderT wo
left join
working.WorkMaterial_nonFiltered wm on wm.WorkOrder = wo.WOId
where
wo.WOId = '00559FB6-4DD2-4762-8DE1-8D1B13962AED'
order by
[MaterialComments]
当我运行时,我得到了11行。第一个的MaterialComments
为NULL
,其他人的数据为NULL
。我并不关心WorkOrderID Comments
00559FB6-4DD2-4762-8DE1-8D1B13962AED 5/17 caj in transit; expected delivery on 5/18 per ups 1Z25AR580319345668
00559FB6-4DD2-4762-8DE1-8D1B13962AED 5/18 caj updated esd on 6/17 per vendor site
00559FB6-4DD2-4762-8DE1-8D1B13962AED 5/17 caj allocated to ship
00559FB6-4DD2-4762-8DE1-8D1B13962AED 5/17 caj updated esd on 5/27 per vendor site
00559FB6-4DD2-4762-8DE1-8D1B13962AED 5/18 caj processed; no udpated delivery date per estes Tracking #/BOL #: 3SNS31780960
00559FB6-4DD2-4762-8DE1-8D1B13962AED 5/18 caj processed; no updated delivery date per ups 1Z39ER600354622348
008FC1D1-D6A6-4E48-A69F-168DBF8D215A Jun 25 2015 10:22AM;dlb223;6/25 dlb 1Z4370490304215160 to be dlv'd today
008FC1D1-D6A6-4E48-A69F-168DBF8D215A Jun 8 2015 1:11PM;klh323;6/8 klh Reserved to meet 06/30 requested delivery date
008FC1D1-D6A6-4E48-A69F-168DBF8D215A Jun 25 2015 10:23AM;dlb223;6/23/2015
008FC1D1-D6A6-4E48-A69F-168DBF8D215A Jun 25 2015 10:23AM;dlb223;6/5 dlb 1Z4370490304215937 to be dlv'd today
008FC1D1-D6A6-4E48-A69F-168DBF8D215A Jun 25 2015 10:24AM;dlb223;6/25 dlb 1Z4370490304216445 to be dlv'd today
00910B84-486C-4AD4-9B1E-5F8D8B42C841 5/12 jad IN TRANSIT; EXPECTED DELIVERY 5/12 PER UPS 1Z750WA20313280446
00910B84-486C-4AD4-9B1E-5F8D8B42C841 4/29 jad IN TRANSIT; EXPECTED DELIVERY 4/29 PER UPS 1Z39ER600354244542
行,除非它是导致问题的原因,而是我真正需要连接的其他行,由“;”分隔。我已经尝试过一步一步建立这个,但我无法弄清楚为什么我总是得到11行而不仅仅是1.我已经调查了以下其他问题来尝试寻找解决方案:
Concatenate many rows into a single text string?
Concatenate Rows using FOR XML PATH() from multiple tables
How to make a query with group_concat in sql server
String Aggregation in the World of SQL Server
到目前为止,我无法从这些链接中获得任何建议,我仍然可以获得11行而不是1行连接。
修改
以下是我在表格中提供的一些示例数据:(请注意如何使这个实际的表格对于草率的格式化而言抱歉)
WorkOrderID Comments
00559FB6-4DD2-4762-8DE1-8D1B13962AED 5/17 caj in transit; expected delivery on 5/18 per ups 1Z25AR580319345668; 5/18 caj updated esd on 6/17 per vendor site; 5/17 caj allocated to ship; 5/17 caj updated esd on 5/27 per vendor site; 5/18 caj processed; no udpated delivery date per estes Tracking #/BOL #: 3SNS31780960; 5/18 caj processed; no updated delivery date per ups 1Z39ER600354622348
008FC1D1-D6A6-4E48-A69F-168DBF8D215A Jun 25 2015 10:22AM;dlb223;6/25 dlb 1Z4370490304215160 to be dlv'd today; Jun 8 2015 1:11PM;klh323;6/8 klh Reserved to meet 06/30 requested delivery date; Jun 25 2015 10:23AM;dlb223;6/23/2015; Jun 25 2015 10:23AM;dlb223;6/5 dlb 1Z4370490304215937 to be dlv'd today; Jun 25 2015 10:24AM;dlb223;6/25 dlb 1Z4370490304216445 to be dlv'd today
00910B84-486C-4AD4-9B1E-5F8D8B42C841 5/12 jad IN TRANSIT; EXPECTED DELIVERY 5/12 PER UPS 1Z750WA20313280446; 4/29 jad IN TRANSIT; EXPECTED DELIVERY 4/29 PER UPS 1Z39ER600354244542
结果我想看起来像这样:(我再也不知道如何把它作为一个表格)
{{1}}
我希望这能让我更加清楚我想要实现的目标。
答案 0 :(得分:0)
我最终使用传递查询到链接服务器。这就是实际问题所在。数据从链接服务器返回。一旦我弄明白,我就能得到我需要的结果。现在我有一个使用for xml
的很长的传递查询,并按照我需要的方式给出了WOID的注释。
答案 1 :(得分:0)
明确要求元素中的文本节点,而不是元素。
select
wo.WOId as ID,
(select distinct '; ' + c.comments as "text()"
from working.Comment c
where c.Instance_InstanceId = wm.id
for xml path(''), type) as "MaterialComments"
from
working.WorkOrderT wo
left join
working.WorkMaterial_nonFiltered wm on wm.WorkOrder = wo.WOId
where
wo.WOId = '00559FB6-4DD2-4762-8DE1-8D1B13962AED'
order by
[MaterialComments]