Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL Server中将多行转换为一行?_Sql_Sql Server_Join_Inner Join_Multirow - Fatal编程技术网

如何在SQL Server中将多行转换为一行?

如何在SQL Server中将多行转换为一行?,sql,sql-server,join,inner-join,multirow,Sql,Sql Server,Join,Inner Join,Multirow,我有一个疑问。假设我有两个名为PersonInfo和PersonEducation的表。我使用StudentId对这些表应用了联接操作,得到了类似的结果 StudentIdId Name University Department Status --------------------------------------------------------------- 1 John Cambridge

我有一个疑问。假设我有两个名为
PersonInfo
PersonEducation
的表。我使用
StudentId
对这些表应用了联接操作,得到了类似的结果

   StudentIdId      Name    University    Department     Status
   ---------------------------------------------------------------
      1             John    Cambridge     Computer       Graduated
      1             John    Berkeley      Mathematic     Graduated
      1             John    Boston        Economy        Ongoing
这只是一个学生的例子(约翰)。它显示约翰从两所大学毕业,仍在一所大学学习。大学人数可能会因学生而异。我的问题是如何在一行中显示这三行。我的意思是我想在一行中显示所有教育信息,以避免一个人有多行


提前感谢您的帮助。

这取决于您对输出的要求

  • 如果希望聚合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;
    
  • 如果你想上一所大学,那么你需要一个有日期参加的领域。有人可能同时上不止一所大学,所以你需要关于如何挑选优胜者的规则。但是,在这些情况下,可以使用子句对数据进行排序并过滤结果

  • 您可以按照所述或在任何数量的其他文章中将数据连接在一起

  • 测试数据 查询 结果
    你先试一试怎么样?你能给我们看一个你希望你的数据是什么样的样本吗?如果约翰在5所大学学习呢?对于任何一个学生可以拥有的大学数量是否有严格的限制?
    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          ║
    ╚═════════════╩══════╩═════════════════════════════╩═══════════════════════════════╩═══════════════════════════════╝