4

我需要根据项目 ID 连接查询的不同行中的值。

例如,我有以下几行数据。

GroupID LABEL   VALUE   UNIT
    1   Name    Henry   
    1   Guest   Manny   Guest
    1   Room    12  
    1   Milk    10      Quart
    1   Eggs    3       dozen
    2   Name    Mark    Supervisor
    2   Water   13      Litre
    2   Milk    3       Gallons
    2   Soap    12      bars

作为输出,我想得到以下

ItemID     VALUE 

1          Name: Henry; Guest:Manny Guest; Room:12; Milk:10 Quart; Eggs: 3 dozen;

2          Name: Mark supervisor; Water: 13 litre; Milk: 3 Gallons; Soap: 12 bars;

请问我该怎么做?

4

4 回答 4

4

您可以基于此示例 ( SQL Fiddle ) 编写递归查询 - 它现在包含处理 UNIT 列中的 NULL 值的代码。

 WITH PRE 
     AS (SELECT *, 
                Row_number() 
                  OVER ( 
                    PARTITION BY GROUPID 
                    ORDER BY GROUPID) RN 
         FROM   TABLE1), 
     RECURSIVE 
     AS (SELECT *, 
                Cast(LABEL + ':' + VALUE + ' ' + Isnull(UNIT, '') + ';' AS 
                     VARCHAR( 
                     MAX)) 
                    AS FINAL 
         FROM   PRE 
         WHERE  RN = 1 
         UNION ALL 
         SELECT t1.*, 
                FINAL 
                + Cast(t1.LABEL +':' + t1.VALUE +' '+ Isnull(t1.UNIT, '') + ';' 
                AS 
                VARCHAR(MAX)) 
         FROM   PRE T1 
                INNER JOIN RECURSIVE T2 
                        ON T1.GROUPID = T2.GROUPID 
                           AND T1.RN = T2.RN + 1) 
SELECT T1.GROUPID, 
       T1.FINAL 
FROM   RECURSIVE T1 
       INNER JOIN (SELECT Max(RN) RN, 
                          GROUPID 
                   FROM   RECURSIVE 
                   GROUP  BY GROUPID) T2 
               ON T1.GROUPID = T2.GROUPID 
                  AND t1.RN = T2.RN  
于 2012-10-12T06:22:38.253 回答
3

检查这一点在 Transact-SQL 中连接行值有很多方法可以解决它。

Declare @t TABLE 
( 
    GroupID INT, 
    Label NVARCHAR(20), 
    Value NVARCHAR(20), 
    Unit NVARCHAR (50) 
) 

INSERT INTO @t 
VALUES   (1,'Name','Henry','') 
        ,(1,'Guest','Manny','Guest') 
        ,(1,'Room','12','') 
        ,(1,'Milk','10','Quart')   
        ,(1,'Eggs','3','dozen')   
        ,(2,'Name','Mark','Supervisor')   
        ,(2,'Water','13','Litre')   
        ,(2,'Milk','3','Gallons')   
        ,(2,'Soap','12','bars')  

;With Cte As
(
    Select
            GroupId
            ,Label = Label + ': ' + Value + ' ' + Unit
    From @t

)

Select 
    GroupId
    ,Value = Stuff((Select ';' + Cast(Label As Varchar(Max))
    From Cte c2
    Where c1.GroupId = c2.GroupId
    For Xml Path('')),1,1,'')+';'

From Cte c1
Group By c1.GroupId

在此处输入图像描述

还要注意Concatenate many rows into a single text string of Stack Overflow

于 2012-10-12T06:33:01.807 回答
3

请试试:

SELECT DISTINCT a.GroupID AS ItemID,(
SELECT ISNULL(b.LABEL,'')+': '+ISNULL(b.VALUE,'')+' '+ISNULL(b.UNIT,'')+';' FROM TABLE b WHERE b.GroupID=a.GroupID
FOR XML PATH('')) AS [VALUE]
FROM TABLE a
于 2012-10-12T06:24:59.823 回答
2

完整的工作示例:

SET NOCOUNT ON
GO

DECLARE @SourceTable TABLE
(
    GroupID INT,
    LABEL NVARCHAR(20),
    VALUE NVARCHAR(20),
    UNIT NVARCHAR (50)
)

INSERT INTO @SourceTable (GroupID,LABEL,VALUE,UNIT)
VALUES   (1,'Name','Henry','')
        ,(1,'Guest','Manny','Guest')
        ,(1,'Room','12','')
        ,(1,'Milk','10','Quart')  
        ,(1,'Eggs','3','dozen')  
        ,(2,'Name','Mark','Supervisor')  
        ,(2,'Water','13','Litre')  
        ,(2,'Milk','3','Gallons')  
        ,(2,'Soap','12','bars') 

SELECT DISTINCT GroupID
       ,(SELECT SUBSTRING((SELECT ';' + LABEL +':'+VALUE+' '+UNIT FROM @SourceTable AS B WHERE A.GroupID=B.GroupID  FOR XML PATH('')),2,200) AS CSV)
FROM @SourceTable AS A

SET NOCOUNT OFF
GO
于 2012-10-12T06:43:03.403 回答