来自此查询的行连接

发布于 2024-08-26 00:57:29 字数 1309 浏览 4 评论 0原文

我有这个查询:

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 技术交流群。

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

发布评论

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

评论(3

把时间冻结 2024-09-02 00:57:29

尝试这样的事情:

DECLARE @TableA  table (EDIFICIOS_ID int, TITULAR nvarchar(500))
INSERT INTO @TableA VALUES (1911 ,'Ana María García')
INSERT INTO @TableA VALUES (1911 ,'Anselmo Piedrahita')
INSERT INTO @TableA VALUES (1911 ,'Manuel López')
INSERT INTO @TableA VALUES (2594 ,'Carlos Pérez')
INSERT INTO @TableA VALUES (2594 ,'Felisa García')
INSERT INTO @TableA VALUES (6865 ,'Carlos Pérez')
INSERT INTO @TableA VALUES (6865 ,'Felisa García')
INSERT INTO @TableA VALUES (8428 ,'Carlos Pérez')

;with ResutSet AS
(
    SELECT EDIFICIOS_ID,TITULAR FROM @TableA
    --replace with your query here and don't use @TableA
    --  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;
)
SELECT
    c1.EDIFICIOS_ID
        ,STUFF(
                 (SELECT
                      '; ' + TITULAR
                      FROM ResutSet  c2
                      WHERE c2.EDIFICIOS_ID=c1.EDIFICIOS_ID
                      ORDER BY c1.EDIFICIOS_ID, TITULAR
                      FOR XML PATH('') 
                 )
                 ,1,2, ''
              ) AS CombinedValue
    FROM ResutSet c1
    GROUP BY c1.EDIFICIOS_ID
    ORDER BY c1.EDIFICIOS_ID

输出:

EDIFICIOS_ID CombinedValue
------------ ---------------------------------------------------
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

(4 row(s) affected)

try something like this:

DECLARE @TableA  table (EDIFICIOS_ID int, TITULAR nvarchar(500))
INSERT INTO @TableA VALUES (1911 ,'Ana María García')
INSERT INTO @TableA VALUES (1911 ,'Anselmo Piedrahita')
INSERT INTO @TableA VALUES (1911 ,'Manuel López')
INSERT INTO @TableA VALUES (2594 ,'Carlos Pérez')
INSERT INTO @TableA VALUES (2594 ,'Felisa García')
INSERT INTO @TableA VALUES (6865 ,'Carlos Pérez')
INSERT INTO @TableA VALUES (6865 ,'Felisa García')
INSERT INTO @TableA VALUES (8428 ,'Carlos Pérez')

;with ResutSet AS
(
    SELECT EDIFICIOS_ID,TITULAR FROM @TableA
    --replace with your query here and don't use @TableA
    --  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;
)
SELECT
    c1.EDIFICIOS_ID
        ,STUFF(
                 (SELECT
                      '; ' + TITULAR
                      FROM ResutSet  c2
                      WHERE c2.EDIFICIOS_ID=c1.EDIFICIOS_ID
                      ORDER BY c1.EDIFICIOS_ID, TITULAR
                      FOR XML PATH('') 
                 )
                 ,1,2, ''
              ) AS CombinedValue
    FROM ResutSet c1
    GROUP BY c1.EDIFICIOS_ID
    ORDER BY c1.EDIFICIOS_ID

OUTPUT:

EDIFICIOS_ID CombinedValue
------------ ---------------------------------------------------
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

(4 row(s) affected)
兔小萌 2024-09-02 00:57:29

这似乎类似于这个问题 - 您可能会发现它对回答您的问题很有用。它专门针对 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?

迷爱 2024-09-02 00:57:29

这不完全是+你要找谁,但 MSSQL 上还有另一种方法
连接值。

    SELECT DISTINCT 
    1 AS TAG,
    NULL AS PARENT,
    EDIFICIOS_ID AS [EDIFICIOS!1!EDIFICIOS_ID],
    NULL AS [EDIFICIOS!1!TITULAR!IDREFS]
FROM 
    @TABLEA
UNION ALL
SELECT DISTINCT  
    1 AS TAG,
    NULL AS PARENT,
    EDIFICIOS_ID AS [EDIFICIOS!1!EDIFICIOS_ID],
    TITULAR+';' AS  [EDIFICIOS!1!TITULAR!IDREFS]
FROM 
    @TABLEA
ORDER BY 
    [EDIFICIOS!1!EDIFICIOS_ID]
FOR XML EXPLICIT

This is not exactly +who are you looking for, but there are another way on MSSQL
to concatenate values.

    SELECT DISTINCT 
    1 AS TAG,
    NULL AS PARENT,
    EDIFICIOS_ID AS [EDIFICIOS!1!EDIFICIOS_ID],
    NULL AS [EDIFICIOS!1!TITULAR!IDREFS]
FROM 
    @TABLEA
UNION ALL
SELECT DISTINCT  
    1 AS TAG,
    NULL AS PARENT,
    EDIFICIOS_ID AS [EDIFICIOS!1!EDIFICIOS_ID],
    TITULAR+';' AS  [EDIFICIOS!1!TITULAR!IDREFS]
FROM 
    @TABLEA
ORDER BY 
    [EDIFICIOS!1!EDIFICIOS_ID]
FOR XML EXPLICIT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文