如何将同一 table 中的多行合并为单行结果?

How to merge multiple rows from the same table into a single row result?

我想弄清楚如何将多个地址合并到一行中w/o复制任何记录。

公司Table

|---------------------|------------------|
|      CompanyId      |     Name         |
|---------------------|------------------|
|          1          |     ABC          |
|---------------------|------------------|
|          2          |     XYZ          |
|---------------------|------------------|

地址类型Table

|---------------------|------------------|
|  AddressTypeId      |   AddressType    |
|---------------------|------------------|
|          1          |     Location     |
|---------------------|------------------|
|          2          |     Shipping     |
|---------------------|------------------|
|          3          |     Billing      |
|---------------------|------------------|

公司地址Table

|---------------------|------------------|------------------|
|  CompanyID          |   AddressId      |   AddressTypeId  |
|---------------------|------------------|------------------|
|          1          |     1            |     1            |
|---------------------|------------------|------------------|
|          1          |     2            |     2            |
|---------------------|------------------|------------------|
|          1          |     2            |     3            |
|---------------------|------------------|------------------|

地址Table

|---------------------|------------------|
|     AddressId       |      Address     |
|---------------------|------------------|
|          1          |     123 Main St  |
|---------------------|------------------|
|          2          |     156 Front St |
|---------------------|------------------|

我想要的结果是这样的:

|-------------|--------|-------------------|-------------------|------------------|
|  CompanyId  |  Name  |  LocationAddress  |  ShippingAddress  |  BillingAddress  |
|-------------|--------|-------------------|-------------------|------------------|
|  1          |  ABC   | 123 Main St       |  156 Front St     |  156 Front St    |
|-------------|--------|-------------------|-------------------|------------------|

我原以为我可以像这样在 CompanyAddressAddress table 上进行多个连接:

SELECT
     c.CompanyId
     , c.CompanyName
     , la.Address as LocationAddress  
     , sa.Address as ShippingAddress  
     , ba.Address as BillingAddress  
FROM
     Company c
     JOIN CompanyAddress al ON ca.CompanyID = c.CompanyID an ca.AddressTypeId = 1
     JOIN CompanyAddress as ON ca.CompanyID = c.CompanyID an ca.AddressTypeId = 2
     JOIN CompanyAddress ab ON ca.CompanyID = c.CompanyID an ca.AddressTypeId = 3
     JOIN Address la ON la.AddressId = al.AddressId
     JOIN Address sa ON sa.AddressId = as.AddressId
     JOIN Address ba ON sa.AddressId = ab.AddressId

但这让我明白了:

|-------------|--------|-------------------|-------------------|------------------|
|  CompanyId  |  Name  |  LocationAddress  |  ShippingAddress  |  BillingAddress  |
|-------------|--------|-------------------|-------------------|------------------|
|  1          |  ABC   | 123 Main St       |  null             |  null            |
|-------------|--------|-------------------|-------------------|------------------|
|  1          |  ABC   | null              |  156 Front St     |  null            |
|-------------|--------|-------------------|-------------------|------------------|
|  1          |  ABC   | null              |  null             |  156 Front St    |
|-------------|--------|-------------------|-------------------|------------------|

我明白为什么会得到这个结果。但是我没有在脑海中看到正确的查询。

您可以进行条件聚合:

select 
    ca.CompanyId,
    ca.CompanyName,
    max(case when at.AddressType = 'Location' then a.Address end) LocationAddress,
    max(case when at.AddressType = 'Shipping' then a.Address end) ShippingAddress,
    max(case when at.AddressType = 'Billing'  then a.Address end) BillingAddress
from Company c
inner join CompanyAddress ca on ca.CompanyId = c.CompanyId
inner join Address a on a.AddressId = ca.AddressId
inner join AddressType at on at.AddressTypeId = ca.AddressTypeId
group by ca.CompanyId , ca.CompanyName