0

我有一个查询问题。假设我有两个名为PersonInfoand的表PersonEducation。我在这些表上应用了连接操作,StudentId得到了这样的结果。

   StudentIdId      Name    University    Department     Status
   ---------------------------------------------------------------
      1             John    Cambridge     Computer       Graduated
      1             John    Berkeley      Mathematic     Graduated
      1             John    Boston        Economy        Ongoing

这只是学生(约翰)的一个例子。这表明约翰毕业于 2 所大学,但仍在一所大学学习。大学人数可能会因学生而异。我的问题是,我怎样才能在 1 行中显示这 3 行。我的意思是我想在一行中显示所有教育信息,以免一个人有多行。

在此先感谢您的帮助。

4

2 回答 2

2

测试数据

DECLARE @TABLE TABLE (StudentIdId INT, Name VARCHAR(100), University VARCHAR(100)
                       , Department VARCHAR(100),[Status] VARCHAR(100))
INSERT INTO @TABLE VALUES 
(1 ,'John','Cambridge','Computer'  ,'Graduated'),
(1 ,'John','Berkeley' ,'Mathematic','Graduated'),
(1 ,'John','Boston'   ,'Economy'   ,'Ongoing'),
(2 ,'Pete','Cambridge','Computer'  ,'Graduated'),
(2 ,'Pete','Berkeley' ,'Mathematic','Graduated')

询问

SELECT t.StudentIdId
      ,t.Name
      ,STUFF((SELECT ', ' + University 
              FROM @TABLE 
              WHERE StudentIdId = t.StudentIdId
              FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)'),1,2,'') AS University
      ,STUFF((SELECT ', ' + Department 
              FROM @TABLE 
              WHERE StudentIdId = t.StudentIdId
              FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)'),1,2,'') AS Department
      ,STUFF((SELECT ', ' + [Status] 
              FROM @TABLE 
              WHERE StudentIdId = t.StudentIdId
              FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)'),1,2,'') AS [Status]

FROM @TABLE t 
GROUP BY t.StudentIdId ,t.Name

结果

╔═════════════╦══════╦═════════════════════════════╦═══════════════════════════════╦═══════════════════════════════╗
║ StudentIdId ║ Name ║         University          ║          Department           ║            Status             ║
╠═════════════╬══════╬═════════════════════════════╬═══════════════════════════════╬═══════════════════════════════╣
║           1 ║ John ║ Cambridge, Berkeley, Boston ║ Computer, Mathematic, Economy ║ Graduated, Graduated, Ongoing ║
║           2 ║ Pete ║ Cambridge, Berkeley         ║ Computer, Mathematic          ║ Graduated, Graduated          ║
╚═════════════╩══════╩═════════════════════════════╩═══════════════════════════════╩═══════════════════════════════╝
于 2014-12-30T21:06:29.917 回答
0

这取决于你想要的输出。

  1. 如果要聚合 PersonEducation 中的数据以计算状态,则可以加入 PersonEducation 表的子查询。有很多关于如何使用子查询以及如何按字段分组的文章。

就像是:

SELECT pere.StudentId
    , pere.StudentName
    , peri.UniversityCount
    , peri.GraduatedStatusCout
    , peri.OngoingStatusCount
FROM PersonInfo peri
LEFT JOIN
    (SELECT StudentId
        , UniversityCount = COUNT(*)
        , GraduatedStatusCount = SUM(IIF(Status = 'Graduated', 1, 0))
        , OngoingStatusCount = SUM(IIF(Status = 'Ongoing', 1, 0))
    FROM PersonEducation
    GROUP BY StudentId) pere
    ON peri.StudentId = pere.StudentId;
  1. 如果您想获得最后一所大学,那么您将需要某个日期就读的领域。有人可能同时就读不止一所大学,因此您需要关于如何挑选获胜者的规则。但是,在这些情况下,您可以在 OVER()子句中使用 ROW_NUMBER() 来对数据进行排序并过滤结果。

  2. 您可以按照此处或任何数量的其他文章中的描述将数据连接在一起。

于 2014-12-30T21:02:43.160 回答