如何连接多个表中的数据?

时间:2014-09-17 20:52:11

标签: sql sql-server-2008 tsql sql-server-2008-r2 sql-server-2012

我有2个表有多对多的关系如下

部门表

╔═══════════╦════════════════╗
║   deptID  ║    DeptName    ║
╠═══════════╬════════════════╣
║   1       ║      abc       ║   
║   2       ║      def       ║       
║   3       ║      ghi       ║
╚═══════════╩════════════════╝

另一个是 标准表

╔════════╦════════════════════════╗
║   id   ║     criteria           ║
╠════════╬════════════════════════╣
║   1    ║  number of Employees   ║
║   2    ║  status                ║
║   3    ║  nature of products    ║
║   .    ║  ---                   ║
║   .    ║  ---                   ║
║   .    ║  ---                   ║
║   .    ║  ---                   ║
║   .    ║  ---                   ║
║   .    ║  ---                   ║
║   .    ║  ---                   ║
╚════════╩════════════════════════╝

在单独的表中以多对多的关系动态增加,为每个部门提供一个值,如下所示

╔════════╦══════╦═════════════════════╗
║ DeptID ║ CrID ║        value        ║
╠════════╬══════╬═════════════════════╣
║ 1      ║ 1    ║ 20                  ║
║ 1      ║ 2    ║ currently active    ║
║ 1      ║ 3    ║ mechanical          ║
║ 1      ║ .    ║ .....               ║
║ 1      ║ .    ║ .....               ║
║ 1      ║ .    ║ .....               ║
║ 2      ║ 1    ║ 40                  ║
║ 2      ║ 2    ║ paused for transfer ║
║ 2      ║ 3    ║ software            ║
║ 2      ║ .    ║ .....               ║
║ 2      ║ .    ║ .....               ║
║ 2      ║ .    ║ .....               ║
║ 3      ║ 1    ║ 50                  ║
║ 3      ║ 2    ║ heavy duty          ║
║ 3      ║ 3    ║ support             ║
║ 3      ║ .    ║ .....               ║
║ 3      ║ .    ║ .....               ║
║ 3      ║ .    ║ .....               ║
║        ║      ║                     ║
║ .      ║ .    ║ .....               ║
║ .      ║ .    ║ .....               ║
║ .      ║ .    ║ .....               ║
║        ║      ║                     ║
╚════════╩══════╩═════════════════════╝

1)如何获得此结果

╔═══════╦═════════════╦═════════════════════╦════════════╦═════╦═════╗
║  name ║  number of  ║       status        ║ nature of  ║ ... ║ ... ║
║       ║  Employees  ║                     ║ products   ║     ║     ║
╠═══════╬═════════════╬═════════════════════╬════════════╬═════╬═════╣
║ abc   ║     20      ║ currently active    ║ mechanical ║ ... ║ ... ║
║ def   ║     40      ║ paused for transfer ║ software   ║ ... ║ ... ║
║ ghi   ║     50      ║ heavy duty          ║ support    ║ ... ║ ... ║
║  .    ║   ..        ║ ...                 ║ ... ...    ║     ║     ║
║  .    ║   ..        ║ ...                 ║ ... ...    ║     ║     ║
║  .    ║   ..        ║ ...                 ║ ... ...    ║     ║     ║
╚═══════╩═════════════╩═════════════════════╩════════════╩═════╩═════╝

2)如何获得一个表如下

╔══════╦═══════════════════════════════════════════════════════════════════════════════════╗
║ name ║                                       notes                                       ║
╠══════╬═══════════════════════════════════════════════════════════════════════════════════╣
║ abc  ║ number of Employees 20 , status currently active , nature of products mechanical  ║
║ def  ║ number of Employees 40 , status paused for transfer , nature of products software ║
║ ghi  ║ number of Employees 50 , status heavy duty, nature of products support            ║
║      ║                                                                                   ║
║ ...  ║ ..................................................                                ║
║ ...  ║ ..................................................                                ║
║ ...  ║ ..................................................                                ║
║      ║                                                                                   ║
╚══════╩═══════════════════════════════════════════════════════════════════════════════════╝

1 个答案:

答案 0 :(得分:2)

(1)

你需要这个结果吗?这将需要使用动态SQL,因为标准的数量未知或未修复,因此结果中的列数不固定

(2)

SELECT d.DeptName,
       notes = stuff(n.notes, 1, 1, '')
FROM   Departments d
       OUTER APPLY
       (
            SELECT ',' + c.Criteria + ' ' + dc.value
            FROM   DeptCriteria dc 
                   INNER JOIN Criteria c      ON dc.CrID  = c.id
            WHERE  dc.DeptID = d.deptID
            ORDER BY c.id
            FOR XML PATH('')
       ) n (notes)
相关问题