简体   繁体   English

SQL服务器:将行转置为列(n:m关系)

[英]SQL server: Transpose Rows to Columns (n:m relationship)

After trying it myself for some hours now I need to ask for help. 在我自己尝试了几个小时后,我需要寻求帮助。 I only did some basic SQL until now. 到目前为止我只做了一些基本的SQL。

I want to solve the following: 我想解决以下问题:

(I have translated a couple of things for you to understand the context) (我翻译了几件事让你理解上下文)

I have three tables: 我有三张桌子:

Workers (Mitarbeiter in German - mitID) 工人 (德语中的Mitarbeiter - mitID)

| mitID | Name   | FamName | DOB        | abtIDref |
|-------|--------|---------|------------|----------|
| 1     | Frank  | Sinatra | 12.12.1915 | 1        |
| 2     | Robert | Downey  | 4.4.1965   | 2        |

INFO: abtIDref is an 1:n relation for the Workplace , but not involved here 信息: abtIDrefWorkplace的1:n关系,但不涉及此处

Skills (Faehigkeiten in German - faeID) 技能 (Faehigkeiten in German - faeID)

| faeID | Descr | time | cost |
|-------|-------|------|------|
| 1     | HV    | 2    | 0    |
| 2     | PEV   | 1    | 0    |
| 3     | Drive | 8    | 250  |
| 4     | Nex   | 20   | 1200 |

Link-List 链表

| linkID | mitIDref | feaIDref | when       |
|--------|----------|----------|------------|
| 1      | 2        | 1        | 27.07.2014 |
| 2      | 2        | 2        | 01.01.2016 |
| 3      | 2        | 3        | 20.01.2016 |
| 4      | 1        | 3        | 05.06.2015 |
| 5      | 1        | 4        | 02.11.2015 |

The desired result is: 期望的结果是:

| mitID | Name   | FamName | DOB        | abtIDref | HV        | PEV        | Drive      | Nex        |
|-------|--------|---------|------------|----------|-----------|------------|------------|------------|
| 1     | Frank  | Sinatra | 12.12.1915 | 1        |           |            | 05.06.2015 | 02.11.2015 |
| 2     | Robert | Downey  | 4.4.1965   | 2        | 27.7.2014 | 01.01.2016 | 20.01.2015 |            |

Alternative it could be: 替代方案可能是:

| mitID | Name   | FamName | DOB        | abtIDref | HV | PEV | Drive | Nex |
|-------|--------|---------|------------|----------|----|-----|-------|-----|
| 1     | Frank  | Sinatra | 12.12.1915 | 1        |    |     | x     | x   |
| 2     | Robert | Downey  | 4.4.1965   | 2        | x  | x   | x     |     |

The goal is that users/admins can add up new skills and someone can see on this resultlist, if a person has this skill. 目标是用户/管理员可以添加新技能,并且有人可以在此结果列表中看到,如果一个人具有此技能。



What did i try: 我尝试了什么:

I've come across multiple examples of dynamic SQL and the pivot function, but I don't know how to use it in my case, because I don't run a function like AVG() or MIN() . 我遇到过动态SQL和pivot函数的多个例子,但我不知道如何在我的情况下使用它,因为我没有运行像AVG()MIN()这样的函数。

I tried it like this: 我试过这样的:

DECLARE @columns AS VARCHAR(MAX);
DECLARE @sql AS VARCHAR(MAX);

select @columns = substring((Select DISTINCT ',' + QUOTENAME(faeID) FROM mdb_Fähigkeiten FOR XML PATH ('')),2, 1000);

SELECT @sql = 'SELECT * FROM mdb_Mitarbeiter
PIVOT 
(
    MAX(Value) 
    FOR mitID IN( ' + @columns + ' )
);';

execute(@sql);

And a second approach was: 第二种方法是:

declare @collist nvarchar(max)
SET @collist = stuff((select distinct ',' + QUOTENAME(Question) 
    FROM #t1 -- your table here
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
select @collist
declare @q nvarchar(max)
set @q = '
select * 
from (
    select 
    Vorname, Bezeichnung, faeIDref
        from (
        select #t1.*, #t2.Answer, #t2.parent
        from #t1
        inner join #t2 on #t1.QID = #t2.QID
    ) as x
) as source
pivot (
    max(Answer)
    for Question in (' + @collist + ')
) as pvt
'

exec (@q)

But TBH I don't get the functions found. 但TBH我没有找到功能。 I hope you can provide me with some guidance what I have to change (or even if I can) achieve this. 我希望你能为我提供一些指导,我必须改变(或者即使我可以)实现这一目标。

I believe the query below is what you are looking for. 我相信下面的查询是您正在寻找的。 Adjust the column and table names as needed to fit your database. 根据需要调整列名和表名以适合您的数据库。

DECLARE @sql AS NVARCHAR(MAX)
DECLARE @cols AS NVARCHAR(MAX)

SELECT @cols= ISNULL(@cols + ',','') + QUOTENAME(Descr)
FROM Faehigkeiten ORDER BY faeID 

SET @sql = N'
    SELECT mitID, Name, FamName, DOB, abtIDref, ' + @cols + '
    FROM (
       SELECT mitID, Name, FamName, DOB, abtIDref, [when], descr 
       FROM Mitarbeiter m
       JOIN [Link-List] l ON m.mitID = l.mitIDref
       JOIN Faehigkeiten f ON f.faeID = l.feaIDref
    ) a
    PIVOT(MAX([when]) FOR descr IN (' + @cols + ')) p'

EXEC sp_executesql @sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM