来自此查询的行连接
我有这个查询:
SELECT DISTINCT IM.EDIFICIOS_ID, TI.TITULAR
FROM IMPORTACION IM
INNER JOIN I_EDIFICIO IE ON IM.IMPORTACION_ID=IE.IMPORTACION_ID
INNER JOIN I_EDIFICIO_TITULAR ET ON IM.IMPORTACION_ID=ET.IMPORTACION_ID AND IE.EDIFICIO_ID=ET.EDIFICIO_ID
INNER JOIN I_TITULAR TI ON IM.IMPORTACION_ID=TI.IMPORTACION_ID AND ET.TITULAR_ID=TI.TITULAR_ID
WHERE TI.TITULAR IS NOT NULL AND TI.TITULAR<>''
ORDER BY IM.EDIFICIOS_ID, TI.TITULAR;
返回这个结果集:
EDIFICIOS_ID TITULAR
------------ ------------------
1911 Ana María García
1911 Anselmo Piedrahita
1911 Manuel López
2594 Carlos Pérez
2594 Felisa García
6865 Carlos Pérez
6865 Felisa García
8428 Carlos Pérez
我想连接每个 EDIFICIOS_ID 的 TITULAR 中的值,所以我得到这个:
EDIFICIOS_ID TITULAR
------------ ------------------
1911 Ana María García; Anselmo Piedrahita; Manuel López
2594 Carlos Pérez; Felisa García
6865 Carlos Pérez; Felisa García
8428 Carlos Pérez
我正在尝试使用 用于 XML 路径技巧。我过去使用过它,但是由于我无法真正理解它是如何工作的,所以我不知道如何将它应用到这个特定的情况。你能给我一些想法吗?
I have this query:
SELECT DISTINCT IM.EDIFICIOS_ID, TI.TITULAR
FROM IMPORTACION IM
INNER JOIN I_EDIFICIO IE ON IM.IMPORTACION_ID=IE.IMPORTACION_ID
INNER JOIN I_EDIFICIO_TITULAR ET ON IM.IMPORTACION_ID=ET.IMPORTACION_ID AND IE.EDIFICIO_ID=ET.EDIFICIO_ID
INNER JOIN I_TITULAR TI ON IM.IMPORTACION_ID=TI.IMPORTACION_ID AND ET.TITULAR_ID=TI.TITULAR_ID
WHERE TI.TITULAR IS NOT NULL AND TI.TITULAR<>''
ORDER BY IM.EDIFICIOS_ID, TI.TITULAR;
that returns this result set:
EDIFICIOS_ID TITULAR
------------ ------------------
1911 Ana María García
1911 Anselmo Piedrahita
1911 Manuel López
2594 Carlos Pérez
2594 Felisa García
6865 Carlos Pérez
6865 Felisa García
8428 Carlos Pérez
I want to concatenate the values from TITULAR for each EDIFICIOS_ID, so I get this:
EDIFICIOS_ID TITULAR
------------ ------------------
1911 Ana María García; Anselmo Piedrahita; Manuel López
2594 Carlos Pérez; Felisa García
6865 Carlos Pérez; Felisa García
8428 Carlos Pérez
I'm trying to use the FOR XML PATH trick. I've used it in the past but, since I can't really understand how it works, I can't figure out how to apply it to this specific case. Can you provide me with some ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的事情:
输出:
try something like this:
OUTPUT:
这似乎类似于这个问题 - 您可能会发现它对回答您的问题很有用。它专门针对 MySQL 而不是 sql server,但无论如何,请检查一下。
编辑:
看起来您可能必须为 SQL Server 模拟 group_concat - 这是一个讨论这个问题的问题。
希望对一些人有所帮助。
编辑 2:
但这确实让我想知道为什么你不只在客户端应用程序上而不是通过 SQL 执行此操作。使用检索到的具有重复 int 值的表来完成此操作似乎足够简单。也许这可能是一种可以采取的方法?
This seems similar to this question on SO - you might find it useful in answering your question. It is specifically about MySQL not sql server, but anyway, check it out.
Edit:
It looks like you may have to simulate group_concat for SQL Server - here's a SO question that discusses that.
Hope that helps some.
Edit 2:
But that does make me wonder why you don't just do this on the client app and not via SQL. It seems it would be straightforward enough to accomplish this with the retrieved table that has duplicated int values. Perhaps that might be an approach to take?
这不完全是+你要找谁,但 MSSQL 上还有另一种方法
连接值。
This is not exactly +who are you looking for, but there are another way on MSSQL
to concatenate values.