MS SQL多次旋转另一个表

时间:2018-04-10 09:47:25

标签: sql sql-server rdbms

我有一个具有以下结构的数据库

ID   Name
1     John
2     Doe
3     Dave
4     Smith

另一张表持有员工亲属

ID EmpID RelativeType Name
1  1      Son         x
2  1      Daughter    y
3  1      Wife        a
4  1      Friend      b
5  1      Father      c
6  1      Friend      e

孩子最多可以固定8个,朋友最多可以固定3个

我能够在下方制作一个透视查询来带孩子,但无法再次转动以获取3个朋友,父亲等。

这是我的疑问:

    select *
from 
(
  select e.ID, e.FirstName,
  CASE WHEN es.RelativeType = 'Son' or es.RelativeType = 'Daughter' THEN es.FirstName END as Child, CASE WHEN es.RelativeType = 'Son' or es.RelativeType = 'Daughter' THEN CONCAT('Child ',ROW_NUMBER() OVER (ORDER BY e.ID)) END as Relation
  from Employee e
  left join EmployeeRelatives es
    on e.ID = es.EmpID
  group by 
 e.ID, e.FirstName,es.RelativeID,es.FirstName
) x
pivot
(
  max(Child)
  for Relation in([Child 1],[Child 2],[Child 3], [Child 4], [Child 5], [Child 6], [Child 7], [Child 8])
) as p1

以上查询返回以下结果。

EmpID , Name, Child 1, Child 2,Child 3, Child 4, Child 5, Child 6
 1      John   x        y       NULL    NULL     NULL     NULL

当我制作多个枢轴时,它会返回多行,这不是必需的。 我需要修改上面的查询以带来所需的结果

必需的结果集如下

EmpID , Name, Child 1, Child 2, Child 3, Child 4, Wife, Friend 1, Friend 2, Friend 3, Father
 1      John   x        y         NULL    NULL      a      b        e          NULL       c

我正在使用MS SQL 2014。

2 个答案:

答案 0 :(得分:0)

我已经使用了所谓的" CROSS PIVOT"这里。

就像我说的,这不是很漂亮;你真的应该在你的表示层做这个。这个解决方案不具备可扩展性,可能很难维护。无论如何,这里是:

CREATE TABLE #Employee (ID int, [Name] varchar(10));

INSERT INTO #Employee
VALUES(1,'John'),
      (2,'Doe'),
      (3,'Dave'),
      (4,'Smith');

CREATE TABLE #Relative (ID int, EmpID int, RelativeType varchar(10), [Name] char(1)); 
INSERT INTO #Relative
VALUES(1,1,'Son','x'),
      (2,1,'Daughter','y'),
      (3,1,'Wife','a'),
      (4,1,'Friend','b'),
      (5,1,'Father','c'),
      (6,1,'Friend','e');

WITH RNs AS (
    SELECT *,
           CASE RelativeType WHEN 'Son' THEN 'Child'
                             WHEN 'Daughter' THEN 'Child'
                             WHEN 'Mother' THEN 'Parent'
                             WHEN 'Father' THEN 'Parent'
                             WHEN 'Wife' THEN 'Spouse'
                             WHEN 'Husband' THEN 'Spouse'
                             ELSE RelativeType END AS Relation,
           ROW_NUMBER() OVER (PARTITION BY EmpID,
                                           CASE RelativeType WHEN 'Son' THEN 'Child'
                                                             WHEN 'Daughter' THEN 'Child'
                                                             WHEN 'Mother' THEN 'Parent'
                                                             WHEN 'Father' THEN 'Parent'
                                                             WHEN 'Wife' THEN 'Spouse'
                                                             WHEN 'Husband' THEN 'Spouse'
                                                             ELSE RelativeType END
                              ORDER BY ID ASC) RN
    FROM #Relative R)

SELECT E.ID AS EmpID,
       E.[Name],
       MAX(CASE WHEN R.Relation = 'Child' AND RN = 1 THEN R.[Name] END) AS Child1,
       MAX(CASE WHEN R.Relation = 'Child' AND RN = 2 THEN R.[Name] END) AS Child2,
       MAX(CASE WHEN R.Relation = 'Child' AND RN = 3 THEN R.[Name] END) AS Child3,
       MAX(CASE WHEN R.Relation = 'Child' AND RN = 4 THEN R.[Name] END) AS Child4,
       MAX(CASE WHEN R.Relation = 'Child' AND RN = 5 THEN R.[Name] END) AS Child5,
       MAX(CASE WHEN R.Relation = 'Child' AND RN = 6 THEN R.[Name] END) AS Child6,
       MAX(CASE WHEN R.Relation = 'Child' AND RN = 7 THEN R.[Name] END) AS Child7,
       MAX(CASE WHEN R.Relation = 'Child' AND RN = 8 THEN R.[Name] END) AS Child8,
       MAX(CASE WHEN R.Relation = 'Spouse' AND RN = 1 THEN R.[Name] END) AS Spouse,
       MAX(CASE WHEN R.Relation = 'Friend' AND RN = 1 THEN R.[Name] END) AS Friend1,
       MAX(CASE WHEN R.Relation = 'Friend' AND RN = 2 THEN R.[Name] END) AS Friend2,
       MAX(CASE WHEN R.Relation = 'Friend' AND RN = 3 THEN R.[Name] END) AS Friend3
       --You get the idea
FROM #Employee E
     JOIN RNs R ON E.ID = R.EmpID
GROUP BY E.ID,
         E.[Name];

GO
DROP TABLE #Employee;
DROP TABLE #Relative;

答案 1 :(得分:0)

您可以尝试一下,我希望这可以帮助您:) 祝好运!! :)

WITH Children AS
(
    SELECT e.EmpID, e.FirstName EmpName, er.FirstName RelName,
            'Child' + CONVERT(NVARCHAR(2), ROW_NUMBER() OVER (PARTITION BY er.EmpID ORDER BY er.RelativeID)) AS 'Relation'
    FROM Employee e
    LEFT JOIN EmployeeRelatives er ON e.EmpID = er.EmpID
    WHERE er.RelativeType IN ('Son', 'Daughter')
),
ChildInRow AS
(
    SELECT *
    FROM Children
    PIVOT
    (
      MAX(RelName)
      FOR Relation in([Child1],[Child2],[Child3], [Child4], [Child5], [Child6], [Child7], [Child8])
    ) AS p1
),
Friends AS
(
    SELECT e.EmpID, e.FirstName EmpName, er.FirstName RelName,
            'Friend' + CONVERT(NVARCHAR(2), ROW_NUMBER() OVER (PARTITION BY er.EmpID ORDER BY er.RelativeID)) AS 'Relation'
    FROM Employee e
    LEFT JOIN EmployeeRelatives er ON e.EmpID = er.EmpID
    WHERE er.RelativeType = 'Friend'
),
FriendsInRow AS
(
    SELECT *
    FROM Friends
    PIVOT
    (
      MAX(RelName)
      FOR Relation in([Friend1],[Friend2],[Friend3])
    ) AS p1
),
AllWife AS
(
    SELECT e.EmpID, e.FirstName EmpName, er.FirstName RelName, 'Wife' AS 'Relation',
            ROW_NUMBER() OVER (PARTITION BY er.EmpID ORDER BY er.RelativeID) wife_row_num
    FROM Employee e
    LEFT JOIN EmployeeRelatives er ON e.EmpID = er.EmpID
    WHERE er.RelativeType = 'Wife'
),
WifeInRow AS
(
    SELECT * FROM AllWife WHERE wife_row_num = 1
),
AllFather AS
(
    SELECT e.EmpID, e.FirstName EmpName, er.FirstName RelName, 'Father' AS 'Relation',
            ROW_NUMBER() OVER (PARTITION BY er.EmpID ORDER BY er.RelativeID) father_row_num
    FROM Employee e
    LEFT JOIN EmployeeRelatives er ON e.EmpID = er.EmpID
    WHERE er.RelativeType = 'Father'
),
FatherInRow AS
(
    SELECT * FROM AllFather WHERE father_row_num = 1
)
SELECT e.EmpID, e.FirstName,
        ISNULL(Child1, '') Child1, ISNULL(Child2, '') Child2, ISNULL(Child3, '') Child3, ISNULL(Child4, '') Child4, ISNULL(Child5, '') Child5, ISNULL(Child6, '') Child6, ISNULL(Child7, '') Child7, ISNULL(Child8, '') Child8,
        ISNULL(w.RelName, '') Wife,
        ISNULL(Friend1, '') Friend1, ISNULL(Friend2, '') Friend2, ISNULL(Friend3, '') Friend3,
        ISNULL(fa.RelName, '') Father
FROM Employee e
FULL OUTER JOIN ChildInRow c ON e.EmpID = c.EmpID
FULL OUTER JOIN FriendsInRow f ON e.EmpID = f.EmpID
FULL OUTER JOIN WifeInRow w ON e.EmpID = w.EmpID
FULL OUTER JOIN FatherInRow fa ON e.EmpID = fa.EmpID

结果应该是这样的: enter image description here