如何在sql中合并两行并生成单行

时间:2013-07-24 06:38:55

标签: c# asp.net sql

我正在努力制作两行并制作单列。行的相同数据必须填充,并且不同的数据必须附加到同一行,

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

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中有没有选择这样做,请帮帮我。

提前致谢。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以使用像这样的查询

select *,(select col1,col2 from table2 where uid=t.uid) from table1 t

答案 2 :(得分:0)

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

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

尝试加入表格,你从两次来自疾病,一次是加入,第二次是右外连接,这样你就可以毫无问题地得到这种疾病。