使用PIVOT表在动态SQL中进行字符串连接

时间:2016-08-03 11:51:18

标签: sql tsql pivot dynamic-sql string-concatenation

我必须处理从ERP系统到MySQL数据库的映射。 ERP系统中存在的结构是:

 _____________________________________
| Article | Feature       | Criterion |
|---------|---------------|-----------|
| Art1    | size          | 4*10      |
| Art1    | color         | red       |
| Art1    | functionality | doesA     |
| Art1    | ...           | ...       |
| Art2    | size          | 2*5       |
| Art2    | color         | green     |
| Art2    | functionality | doesB     |
| Art2    | ...           | ...       |
 -------------------------------------

我需要做的就是这样映射:

 ________________________________________________
| Article | size | color | functionality | ...   |
|---------|------|-------|---------------|-------|
| Art1    | 4*10 | red   | doesA         | ...   |
| Art2    | 2*5  | green | doesB         | ...   |
 ------------------------------------------------

我可以通过T-SQL访问ERP系统,并可以执行一个有效的动态查询,它为我提供了一个表格,如下所示:

DECLARE @cols AS nvarchar(MAX),
@query  AS nvarchar(MAX)


SELECT @cols =  stuff((SELECT DISTINCT ', ' + quotename(f.Feature) + ''
FROM CRITERION c, FEATURE f
WHERE  --necessary joins

FOR xml PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')


SET @query = N'SELECT Article, ' + @cols + N' 
    FROM (
    SELECT Article, Feature, Criterion
    FROM --necessary tables
    WHERE --necessary joins
    ) x

    pivot 
    (
        max(Criterion)
        FOR Feature IN (' + @cols + N')
    ) p                
    '

EXEC sp_executesql @query;

现在出现的问题是,系统会针对某些功能进行多项选择:

 _____________________________________
| Article | Feature       | Criterion |
|---------|---------------|-----------|
| Art3    | color         | red       |
| Art3    | color         | green     |
 -------------------------------------

,查询只是给了我表格中的第一个结果。

 ________________________________________
| Article | size | color | functionality |
|---------|------|-------|---------------|
| Art3    | ...  | red   | ...           |
 ----------------------------------------

所以我的问题是,如果有任何方法可以在子查询'x'或数据透视表'p'中添加字符串连接,那么结果将变为以下:

 _____________________________________________
| Article | size | color      | functionality |
|---------|------|------------|---------------|
| Art3    | ...  | red, green | ...           |
 ---------------------------------------------

2 个答案:

答案 0 :(得分:0)

GROUP BY功能首先使用相同的FOR XML技巧。

SET @query = N'SELECT Article, ' + @cols + N' 
    FROM (
      SELECT Article,  Criterion, 
      Feature = stuff(
        (SELECT '',''+ t2.Feature 
         FROM ttt as t2 
         WHERE t2.Article = t1.Article AND
            t2.Criterion = t1.Criterion
         FOR XML PATH(''))
       ,1,1,'''')
     FROM ttt t1
     GROUP BY Article,  Criterion    
    ) x

    pivot 
    (
        max(Criterion)
        FOR Feature IN (' + @cols + N')
    ) p                
    '

用实际数据源替换ttt。

答案 1 :(得分:0)

@Serg有正确的想法,但字段似乎已关闭。这应该更接近。

SET @query = N'
    SELECT Article, ' + @cols + N' 
    FROM (
            SELECT  Article, 
                    Feature,
                    Criterion = STUFF(
                                        (SELECT '', '' + t2.Criterion 
                                         FROM   t1 as t2 
                                         WHERE  t2.Article = t1.Article 
                                                AND t2.[Feature] = t1.[Feature]
                                         FOR XML PATH('''')), 1, 2,'''')
            FROM (SELECT    Article, Feature, Criterion
                  FROM      --necessary tables
                  WHERE     --necessary joins) t1
        ) x
    pivot 
    (
        MAX(Criterion)
        FOR Feature IN (' + @cols + N')
    ) p                
    '