无限数量的连接数

时间:2013-03-19 07:28:19

标签: sql sql-server dynamic join

我正试图无限次地加入一张桌子。 列是:

+----------+---------+         +--------+--------+--------+---+-----+-----+-----+
| ClientID |  Data   |         | Client |  Data  | Client | * | Cli |  *  | ... |
+----------+---------+         +--------+--------+--------+---+-----+-----+-----+
|  7       | *data*  |         |    7   | *data* |    8   | * |  9  |  *  |     |
|  7       | *data*  |         |    7   | *data* |    8   | * |  9  |  *  |     |
|  8       | *data*  |    ->   |    7   | *data* |    8   | * |  9  |  *  |     |
|  9       | *data*  |         |    7   | *data* |   ...  | * | ... |     |     |
|  ...     |  ...    |         |   ...  |   ...  |        | * |     |     |     |
+--------------------+         +--------+--------+--------+--+-----+------+-----+

我不知道不同客户的数量,我需要表格FULLy JOINed。我尽量不使用动态SQL,我怀疑这是最简单的方法。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

也许

select distinct clientid, 
stuff((select ','+DATA 'data()'
from [table] t1
where t1.clientid=t2.clientid
for xml path('')),1,1,'')
from [table] t2