SQL表行到列 - 可能的PIVOT?

Val*_*nte 4 sql sql-server pivot

我有一个来自Active Direcory的信息表,但不幸的是它显示如下;

+------+------------+----------------+-------------------+
| dnId | propNameId |  propertyName  |   propertyValue   |
+------+------------+----------------+-------------------+
|    1 |         10 | objectsid      | S-1-5-32-548      |
|    1 |         19 | _objectclass   | group             |
|    1 |         80 | cn             | Account Operators |
|    1 |         82 | samaccountname | Account Operators |
|    1 |         85 | name           | Account Operators |
|    2 |         10 | objectsid      | S-1-5-32-544      |
|    2 |         19 | _objectclass   | group             |
|    2 |         80 | cn             | Administrators    |
|    2 |         82 | samaccountname | Administrators    |
|    2 |         85 | name           | Administrators    |
|    3 |         10 | objectsid      | S-1-5-32-551      |
|    3 |         19 | _objectclass   | group             |
|    3 |         80 | cn             | Backup Operators  |
|    3 |         82 | samaccountname | Backup Operators  |
|    3 |         85 | name           | Backup Operators  |
+------+------------+----------------+-------------------+
Run Code Online (Sandbox Code Playgroud)

使用一堆临时表和连接,我可以让它显示我想要的,就像这样;

+------+--------------+--------------+-------------------+-------------------+-------------------+
| dnId |  objectsid   | _objectclass |        cn         |  samaccountname   |       name        |
+------+--------------+--------------+-------------------+-------------------+-------------------+
|    1 | S-1-5-32-548 | group        | Account Operators | Account Operators | Account Operators |
|    2 | S-1-5-32-544 | group        | Administrators    | Administrators    | Administrators    |
|    3 | S-1-5-32-551 | group        | Backup Operators  | Backup Operators  | Backup Operators  |
+------+--------------+--------------+-------------------+-------------------+-------------------+
Run Code Online (Sandbox Code Playgroud)

但是这感觉非常麻烦而且啰嗦..我敢肯定必须有更优雅的方式来做这件事,也许是使用PIVOT,我已经尝试过(并且失败)了解.

有什么想法/建议?

编辑:道歉,删除了mysql标签(这是自动建议).我正在使用MS SQL Server 2008 R2.

Tar*_*ryn 5

您没有指定正在使用的数据库,但以下内容适用于SQL Server或MySQL.您可以使用带有CASE表达式的聚合函数来创建新的列名:

select
  dnid,
  max(case when propertyName = 'objectsid' then propertyValue end) objectsid,
  max(case when propertyName = '_objectclass ' then propertyValue end) objectclass ,
  max(case when propertyName = 'cn' then propertyValue end) cn,
  max(case when propertyName = 'samaccountname' then propertyValue end) samaccountname,
max(case when propertyName = 'name' then propertyValue end) name
from yourtable
group by dnid;
Run Code Online (Sandbox Code Playgroud)

请参阅SQL Fiddle with Demo