【问题标题】:Join two tables returning all rows as single row from the second table连接两个表,从第二个表返回所有行作为单行
【发布时间】:2019-03-18 03:30:42
【问题描述】:

我想从两个具有一对多关系的表中获取单行数据。

  1. 主表
  2. 辅助表

我知道对于主表的每条记录,从表最多可以有 10 行。这是表的结构

主表

 -------------------------------------------------
| ImportRecordId   |       Summary                |
--------------------------------------------------
|        1         |       Imported Successfully  |
|        2         |       Failed                 |
|        3         |       Imported Successfully  |
 -------------------------------------------------

辅助表

 ------------------------------------------------------
| ImportRecordId   |       CodeName   |  CodeValue     |
-------------------------------------------------------
|        1         |       ABC        |  123456A       | 
|        1         |       DEF        |  8766339       |
|        1         |       GHI        |  887790H       |
 ------------------------------------------------------

我想编写一个带有内部联接的查询,以便从两个表中获取数据,即从辅助表中的每一行都应被视为列,而不是显示为多行。 我可以硬编码 20 列名称(因为辅助表中最多可以存在 10 条记录,并且我想在一行中显示两列的值)所以如果辅助表中的记录少于 10 条,则所有其他列将显示为空值。

这是预期的输出。您可以看到,对于主表中的第一条记录,只有三行,这就是为什么这三行中的两个必需列被转换为列,而所有其他列的值都是空。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| ImportRecordId |        Summary        | CodeName1 |  CodeValue1 |  CodeName2 | CodeValue2 |  CodeName3 |  CodeValue3 |  CodeName4 |  CodeValue4|  CodeName5 |  CodeValue5|  CodeName6 |  CodeValue6|  CodeName7 |  CodeValue7 |  CodeName8 |  CodeValue8 | CodeName9 |  CodeValue9 | CodeName10 |  CodeValue10|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|        1       | Imported Successfully |    ABC    |    123456A  |    DEF    |    8766339  |     GHI    |  887790H    |    NULL    |    NULL    |    NULL     |    NULL    |    NULL    |    NULL   |    NULL    |    NULL     |    NULL     |    NULL    |    NULL   |    NULL     |    NULL    |    NULL     |
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

这是我的简单 SQL 查询,它返回两个表中的所有数据,而是从辅助表中返回多行,我希望将它们放在单行中,如上面的结果集。

Select p.ImportRecordId,p.Summary,s.*
from [dbo].[primary_table] p
inner join [dbo].[secondary_table] s on p.ImportRecordId = s.ImportRecordId

【问题讨论】:

  • 您正在寻找的是pivot。网上有很多例子。
  • 实际上,我不必从表中的数据中创建列,我只需要将值与硬编码列相对。

标签: sql sql-server


【解决方案1】:

以下使用 Row_Number()、一个 JOIN 和一个 CROSS APPLY 来创建 PIVOT 的源

您必须添加 CodeName/Value 4...10

示例

Select *
 From  (
        Select A.[ImportRecordId]
              ,B.Summary
              ,C.*
         From (
                Select *
                      ,RN = Row_Number() over (Partition by [ImportRecordId] Order by [CodeName])
                 From   Secondary A
              ) A
         Join   Primary B on A.[ImportRecordId]=B.[ImportRecordId]
         Cross Apply (values (concat('CodeName' ,RN),CodeName)
                            ,(concat('CodeValue',RN),CodeValue)
                     ) C(Item,Value)
       ) src
 Pivot (max(value) for Item in (CodeName1,CodeValue1,CodeName2,CodeValue2,CodeName3,CodeValue3) ) pvt

退货

ImportRecordId  Summary                 CodeName1   CodeValue1  CodeName2   CodeValue2  CodeName3   CodeValue3
1               Imported Successfully   ABC         123456A     DEF         8766339     GHI         887790H

【讨论】:

  • 非常感谢您的帮助。像魅力一样工作。
  • @Asad 乐于助人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多