sql查询从一行的同一列中获取多个值

时间:2013-10-21 16:07:24

标签: sql

如果他们有多个联系人值,我会得到每个员工多行,但我只希望每个员工一行列出他们的姓氏,名字,部门名称,电子邮件和电话。

所以它应该像这样

    First Name    Last Name    Division     Email                  Phone
       Test          Guy         Exec     test@gmail.com        555-5555

这是我所拥有的,但它不起作用:

    SELECT sr.LastName, sr.FirstName, dd.Name, 
        Email = (select sc.ContactValue FROM StaffContactInformation as sc
         INNER JOIN StaffRoster as roster on sc.StaffID = roster.ID
         where sc.ContactTypeID = 3 and roster.ID = sr.ID),
Phone = (SELECT sc1.ContactValue FROM StaffContactInformation as sc1 
        INNER JOIN StaffRoster as roster on sc1.StaffID = roster.ID
        where sc1.ContactTypeID = 1) 
    FROM StaffRoster as sr
    left join dictDivisions as dd on sr.DivisionID = dd.Id  
    left join StaffContactInformation as sci on sr.ID = sci.StaffID
    inner join dictStaffContactTypes as dsct on sci.ContactTypeID = dsct.ID 
    where (sr.Active = 1 and sr.isContractor = 0 )
    ORDER BY sr.LastName, sr.FirstName

我用以下查询解决了这个问题:

    SELECT sr.LastName, sr.FirstName, dd.Name, 
        Email = (select sc.ContactValue FROM StaffContactInformation as sc
         INNER JOIN StaffRoster as roster on sc.StaffID = roster.ID
         where sc.ContactTypeID = 3 and roster.ID = sr.ID),
Phone = (SELECT sc.ContactValue FROM StaffContactInformation as sc 
        INNER JOIN StaffRoster as roster on sc.StaffID = roster.ID
        where sc.ContactTypeID = 1 and roster.ID = sr.ID) 
    FROM StaffRoster as sr
    left join dictDivisions as dd on sr.DivisionID = dd.Id 
    where (sr.Active = 1 and sr.isContractor = 0 )
    ORDER BY sr.LastName, sr.FirstName

2 个答案:

答案 0 :(得分:0)

如果您在子查询中选择emailphone,则可能不需要这两个联接:

left join StaffContactInformation as sci on sr.ID = sci.StaffID
inner join dictStaffContactTypes as dsct on sci.ContactTypeID = dsct.ID

因为它们,您获得的行数与特定人员的联系人数量相同。

最终查询可能如下所示:

SELECT sr.LastName, sr.FirstName, dd.Name, 
    Email = (
        select sc.ContactValue FROM StaffContactInformation as sc
        INNER JOIN StaffRoster as roster on sc.StaffID = roster.ID
        where sc.ContactTypeID = 3 and roster.ID = sr.ID
    ),
    Phone = (
        SELECT sc1.ContactValue FROM StaffContactInformation as sc1 
        INNER JOIN StaffRoster as roster on sc1.StaffID = roster.ID
        where sc1.ContactTypeID = 1
    ) 
FROM StaffRoster as sr
left join dictDivisions as dd on sr.DivisionID = dd.Id  
where (sr.Active = 1 and sr.isContractor = 0 )
ORDER BY sr.LastName, sr.FirstName

答案 1 :(得分:0)

如果您运行的是SQL Server 2005+,则可以使用数据透视表。在这里,我声明一个CTE来封装数据透视表,您可以使用该表连接到查询的其余部分:

;with ContactInfo( StaffId, Email, Phone )
as
(
    select
        sci.StaffID
        , [3] Email
        , [1] Phone
    from
        StaffContractInformation sci
        PIVOT( MAX(ContactValue) for ContactTypeId in ( [3], [1] ) PivotTable

)

SELECT 
    sr.LastName, 
    sr.FirstName, 
    dd.Name, 
    ci.Email,
    ci.Phone
FROM 
    StaffRoster as sr
    left join dictDivisions as dd 
     on sr.DivisionID = dd.Id  
    left join ContactInfo ci
     on sr.ID = ci.StaffID
  --  inner join dictStaffContactTypes as dsct 
     --on sci.ContactTypeID = dsct.ID 
where 
    sr.Active = 1 
    and sr.isContractor = 0
ORDER BY 
    sr.LastName, 
    sr.FirstName