合并具有1个不同列和其余列的行的最佳方法是相同的

时间:2014-10-01 09:22:00

标签: sql sql-server

我已经使用了"coalesce",但我怀疑是否还有其他方法可以使这种查询在性能方面更加优化。

查询是:

declare @table1 table (sno int, postNo int, yearVal int)
insert into @table1 values 
(1001,500001,2001),
(1002,500001,2003),(1003,500001,2009),(1004,500002,2007),
(1005,500003,2006),(1006,500003,2007)

declare @table2 table (sno int, postNo int, postDescription varchar(20))
insert into @table2 values 
(2001,500001,'postDescription 1'),
(2002,500002,'postDescription 2'),
(2003,500003,'postDescription 3')

select t1.postNo,t1.yearVal,t2.postDescription from @table1 t1
left join @table2 t2 on (t1.postNo = t2.postNo)

当前输出:

postNo  yearVal postDescription
500001  2001    postDescription 1
500001  2003    postDescription 1
500001  2009    postDescription 1
500002  2007    postDescription 2
500003  2006    postDescription 3
500003  2007    postDescription 3

postNo  yearVal         postDescription
500001  2001,2003,2009  postDescription 1
500002  2007            postDescription 2
500003  2006,2007       postDescription 3

2 个答案:

答案 0 :(得分:0)

SELECT t1.postNo,
   t2.postDescription,
   (SELECT CONVERT(VARCHAR, yearVal) + ','
    FROM   @table1 b
    WHERE  b.postNo = t1.postNo
    FOR XML PATH(''))
FROM   @table1 t1
   LEFT JOIN @table2 t2
          ON ( t1.postNo = t2.postNo )
GROUP  BY t1.postNo,
      postDescription 

解决问题的另一种方法

SELECT t1.postNo,
   t2.postDescription,
   Max(LEFT(d.yearVal, Len(d.yearVal) - 1))
FROM   @table1 t1
   LEFT JOIN @table2 t2
          ON ( t1.postNo = t2.postNo )
   CROSS APPLY (SELECT CONVERT(VARCHAR, yearVal) + ','
                FROM   @table1 b
                WHERE  b.postNo = t1.postNo
                FOR XML PATH('')) D ( yearVal )
GROUP  BY t1.postNo,
      postDescription 

答案 1 :(得分:0)

<强>演示

SQL Fiddle

<强>查询

SELECT postNo
    ,postDescription
    ,STUFF((
            SELECT ' ,' + cast(yearVal AS VARCHAR)
            FROM @table1
            WHERE postNo = res.postNo
            FOR XML PATH('')
                ,TYPE
            ).value('.', 'NVARCHAR(MAX)'), 1, 2, '') AS yearVal
FROM (
    SELECT t1.postNo
        ,t1.yearVal
        ,t2.postDescription
    FROM @table1 t1
    LEFT JOIN @table2 t2 ON (t1.postNo = t2.postNo)
    ) res
GROUP BY postNo
    ,postDescription

<强>输出

postNo  postDescription     yearVal
500001  postDescription 1   2001 ,2003 ,2009
500002  postDescription 2   2007
500003  postDescription 3   2006 ,2007