从此查询中行连续

时间:2010-03-24 12:53:55

标签: sql-server sql-server-2005 for-xml

我有这个问题:

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

我正在尝试使用FOR XML PATH trick。我过去曾经使用它,但由于我无法理解它是如何工作的,我无法弄清楚如何将它应用于这个特定的情况。你能给我一些想法吗?

3 个答案:

答案 0 :(得分:3)

尝试这样的事情:

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)

答案 1 :(得分:0)

这与SO上的this question类似 - 您可能会发现它在回答您的问题时很有用。它具体是关于MySQL而不是sql server,但无论如何,请查看它。


编辑:

看起来您可能需要为SQL Server模拟group_concat - here's a SO question that discusses that

希望有所帮助。


编辑2:

但这确实让我想知道你为什么不在客户端应用程序上而不是通过SQL执行此操作。看起来使用具有重复的int值的检索表来完成此操作似乎很简单。也许这可能是一种采取的方法?

答案 2 :(得分:0)

这不完全是+你在寻找谁,但在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