Concatenate multiple rows from multiple tables

前端 未结 2 1269
后悔当初
后悔当初 2021-01-07 06:02

I\'ve reviewed many other posts on here and have become pretty familiar with the Coalesce function, but I haven\'t been able to figure out how to do this specific task.

2条回答
  •  生来不讨喜
    2021-01-07 06:56

    I bumped into similar problem before - and the only way I could resolve this (without using cursors), is by creating a CLR aggregate function. Here's an example in C# (and in VB): http://technet.microsoft.com/en-us/library/ms131056(v=SQL.90).aspx

    I believe it just does what you need: concatenation.

    Combining your example and the CLR, to achieve what you want - the SQL would look like:

    SELECT
      c.CommissionPercent
      , dbo.MyAgg(cat.Category)
    FROM #tCommissions AS c
    JOIN #tCategories AS cat ON c.CategoryID = cat.CategoryID
    group by c.CommissionPercent
    

提交回复
热议问题