-4

我正在尝试合并两行并制作单列。行的相同数据必须marge,不同的数据必须附加到同一行,

示例:我从加入三个这样的表中得到结果

uid     name    diseaseid     intensity
1       xxxx    1              high
1       xxxx    2              low

现在我需要这样的最终结果

uid    name      diseaseid1   intensity       diseaseid2        intensity
1      xxxx       1             high           2                  low

是否有任何选项可以在 Sql 中进行这样的操作,请帮助我。

提前致谢。

4

3 回答 3

0

尝试这个

CREATE TABLE #Table1
    ([uid] int, [name] varchar(4), [diseaseid] int, [intensity] varchar(4))
;

INSERT INTO #Table1
    ([uid], [name], [diseaseid], [intensity])
VALUES
    (1, 'xxxx', 1, 'high'),
    (1, 'xxxx', 2, 'low')
;

SELECT MAX([uid]) AS [uid]
       ,MAX([name]) AS [name]
       ,MAX([diseaseid1]) AS [diseaseid1]
       ,MAX([intensity1]) AS [intensity1]
       ,MAX([diseaseid2]) AS [diseaseid2]
       ,MAX([intensity2]) [intensity2]
FROM 
(
    SELECT [uid], [name]
    , CASE WHEN rn=2 THEN NULL ELSE [diseaseid] END AS [diseaseid1]
    , CASE WHEN rn=2 THEN NULL ELSE [intensity] END AS [intensity1]
    , CASE WHEN rn=1 THEN NULL ELSE [diseaseid] END AS [diseaseid2]
    , CASE WHEN rn=1 THEN NULL ELSE [intensity] END AS [intensity2]
    FROM
    (
        SELECT [uid], [name], [diseaseid], [intensity], 
        ROW_NUMBER() OVER(PARTITION BY [uid] ORDER BY Name) AS rn
        FROM #Table1
    ) T
) T
GROUP BY [uid], [name]

DROP TABLE #Table1

EIDT

试试这个动态列

CREATE TABLE #Table1
    ([uid] int, [name] varchar(10), [diseaseid] int, [intensity] varchar(10))


INSERT INTO #Table1
    ([uid], [name], [diseaseid], [intensity])
VALUES
    (1, 'xxxx', 1, 'high'),
    (1, 'xxxx', 2, 'low'),
    (1, 'xxxx', 3, 'medium')


DECLARE @MaxRows INT
DECLARE @CurrentRow INT
DECLARE @Query VARCHAR(MAX)

SET @CurrentRow = 1

SELECT @MaxRows = MAX(cnt) FROM (
SELECT COUNT(name) cnt FROM #Table1 GROUP BY [uid]
) AS T


SET @Query = '
SELECT  MAX([uid]) AS [uid]
       ,MAX([name]) AS [name]'

WHILE @CurrentRow <= @MaxRows
BEGIN
     SET @Query= @Query +  '
                ,MAX(diseaseid'+CONVERT(VARCHAR(10),@CurrentRow)+') AS diseaseid'+CONVERT(VARCHAR(10),@CurrentRow)+''
     SET @Query= @Query +  '
                ,MAX(intensity'+CONVERT(VARCHAR(10),@CurrentRow)+') AS intensity'+CONVERT(VARCHAR(10),@CurrentRow)+''
    SET @CurrentRow = @CurrentRow + 1

END

SET @Query= @Query + ' FROM 
  (
    SELECT [uid], [name]'

SET @CurrentRow = 1

WHILE @CurrentRow <= @MaxRows
BEGIN
     SET @Query= @Query +  '
                , CASE WHEN rn='+CONVERT(VARCHAR(10),@CurrentRow)+' THEN [diseaseid] ELSE NULL END AS diseaseid'+CONVERT(VARCHAR(10),@CurrentRow)+''
     SET @Query= @Query +  '
                , CASE WHEN rn='+CONVERT(VARCHAR(10),@CurrentRow)+' THEN [intensity] ELSE  NULL END AS intensity'+CONVERT(VARCHAR(10),@CurrentRow)+''

     SET @CurrentRow = @CurrentRow + 1
END

SET @Query= @Query + ' FROM 
    (
        SELECT [uid], [name], [diseaseid], [intensity], 
        ROW_NUMBER() OVER(PARTITION BY [uid] ORDER BY Name) AS rn
        FROM #Table1
    ) T2
) T3
GROUP BY [uid], [name]'

PRINT (@Query)

EXEC (@Query)

DROP TABLE #Table1
于 2013-07-24T07:27:54.277 回答
0

我认为你做错了你的加入......看起来你正在做一个你的联盟,你只是按 UID 分组,名字。在不知道连接的情况下,我能给你的最好建议是尝试使用右外连接和左外连接。

我认为这应该做你想要的。

尝试将您从 diseaseid 获得的表加入两次,一次使用 join,第二次使用右外连接,这样您应该可以毫无问题地获得 diseaseid2。

于 2013-07-25T11:30:11.373 回答
0

您可以使用这样的查询

select *,(select col1,col2 from table2 where uid=t.uid) from table1 t
于 2013-07-24T10:31:15.817 回答