使用主记录将不同类型的数据查询到单行

时间:2016-08-17 19:11:25

标签: sql sql-server sql-server-2012

我刚刚创建了以下示例数据来演示我的目标。

enter image description here enter image description here 我需要查询上面的表格,将我的信息作为单行

即。如下 enter image description here

我计划创建两个临时表并单独插入两种不同类型的地址。然后,内部将它们与主公司表连接起来。我不确定,这是一个很好的解决方案。如果有人与我的问题分享他们的想法或代码,我会表示同意。

3 个答案:

答案 0 :(得分:2)

您可以使用两个外部联接进行简单的选择。请注意,您需要连接是外部的,因为对于某些公司,您可能只有一个地址。

DECLARE @company TABLE (
CompanyId int,
CompanyName varchar(50)
)

DECLARE @companyAddress TABLE (
Id int,
AddressType varchar(10),
Address1 varchar(50),
Address2 varchar(50),
Postcode varchar(10),
CompanyId int
)

INSERT INTO @company VALUES (1, 'Test Company')

INSERT INTO @companyAddress VALUES (1, 'Home', '25 Street', 'City 1', 'BA3 1PE', 1)
INSERT INTO @companyAddress VALUES (2, 'Office', '25 Street', 'City 2', 'NA1 4TW', 1)

SELECT c.CompanyId, c.CompanyName, 
h.Address1 AS HomeAddress1, h.Address2 AS HomeAddress2, h.Postcode AS HomePostcode,
o.Address1 AS OfficeAddress1, o.Address2 AS OfficeAddress2, o.Postcode AS OfficePostcode
FROM @company c 
LEFT JOIN
@companyAddress h ON h.CompanyId = c.CompanyId AND h.AddressType = 'Home'
LEFT JOIN
@companyAddress o ON o.CompanyId = c.CompanyId AND o.AddressType = 'Office'

答案 1 :(得分:2)

试试这个..

 Select  companyId,CompanyName,homesddress1
   ,homeaddress2,HomePostCode,OfficeAddress1,OfficeAddress2,OfficePostCode
   From tblCompany a
    Outer apply ( select address1 homesddress1, address2 homeaddress2,postcode HomePostCode 
                          From tblAddress t
                          Where AddressType='home' and t.companyid=a.companyid) 
     Outer apply (select address1 OfficeAddress1, address2 Officeaddress2,postcode OfficePostCode 
                           From tblAddress t2
                            Where AddressType='Office ' and t2.companyid=a.companyid) 

答案 2 :(得分:0)

这是一个Almost Dynamic版本。您只需在最终的数据透视中指定字段列表

Declare @YourTable table (ID int,AddressType varchar(25),Address1 varchar(50),Address2 varchar(50),CompanyID int)
Insert Into @YourTable values 
(1,'Home'  ,'25 Street','City 1',1),
(2,'Office','10 Avenue','City 2',1)
Declare @XML xml = (Select * from @YourTable for XML RAW)               --<<< Initial Query
;with cteBase as (
    Select ID          = R.value('@CompanyID','int')                        --<<< Key ID
          ,AddressType = R.value('@AddressType','varchar(50)')
          ,Item        = R.value('@AddressType','varchar(50)')+Attr.value('local-name(.)','varchar(100)')
          ,Value       = Attr.value('.','varchar(max)') 
     From  @XML.nodes('/row') as A(R)
     Cross Apply A.r.nodes('./@*[local-name(.)!="CompanyID"]') as B(Attr)   --<<< Key ID
),cteDist as (Select Distinct ID,Item from cteBase
),cteComp as (
       Select A.*,B.Value 
        From  cteDist A 
        Cross Apply (Select Value=Stuff((Select Distinct ',' + Value 
                      From  cteBase 
                      Where ID=A.ID 
                        and Item=A.Item 
                      For XML Path ('')),1,1,'') ) B
)
Select * 
 From (Select * From  cteComp) as s
 Pivot (max(value) 
 For Item in (HomeID,HomeAddressType,HomeAddress1,HomeAddress2,OfficeID,OfficeAddressType,OfficeAddress1,OfficeAddress2)) as pvt

返回

ID  HomeID  HomeAddressType HomeAddress1    HomeAddress2    OfficeID    OfficeAddressType   OfficeAddress1  OfficeAddress2
1   1       Home            25 Street       City 1          2           Office             10 Avenue        City 2