在SQL Server查询中选择列的多行

时间:2018-11-19 11:29:46

标签: sql sql-server

我有一个表,我们称它为Case,案例表包含有关案例的基本信息,例如CaseNumberCaseOwner

在单独的表中,CaseDetails中存储了与特定案例关联的许多电话号码。 Type中的CaseDetails列代表电话号码的类型,例如家庭,移动或工作。这两个表链接在CaseNumber上。

更清楚:

案例

CaseNumber     CaseOwner
------------------------
1              Bob
2              Jim
3              Gary

案例详细信息

CaseNumber     Detail        Type
----------------------------------
1              0123456789    1
1              1111111111    2
2              2222222222    1
1              0101001011    3
2              1234123412    2
3              0000011111    1
3              1231231231    2

我希望能够编写一个查询,以查询案件的基本详细信息以及所有相关的电话号码。

在我的脑海中,我想象查询会像以下内容一样

Select 
    CaseNumber, CaseOwner, 
    Detail where Type = 1, Detail where Type = 2, Detail where Type = 3
From
    Case
Join 
    CaseDetails on Case.CaseNumber = CaseDetails.CaseNumber

这样,可以使用type列从CaseDetails表中提取每个Detail。但这在语法上是不正确的,将无法执行。

我将如何构造查询以提取此信息?我不确定在Google上找不到有关此信息的信息,因为我不确定要搜索什么。

这样做的主要目的是让我可以找到特定案例的所有关联编号,并将它们存储在一个位置。

这就是我希望最终输出看起来像

CaseNumber    CaseOwner    Detail1       Detail2        Detail3
-------------------------------------------------------------------
1             Bob          0123456789    1111111111     0000011111

4 个答案:

答案 0 :(得分:2)

您可以在下面尝试使用CASE WHEN表达式

Select a.CaseNumber, CaseOwner, max(case when Type = 1 then detail end) as as detail1, max(case when Type = 2 then detail end) as detail2, max(case when Type = 3 then detail end) as detail3
From Case a
Join CaseDetails b on a.CaseNumber = b.CaseNumber
group by a.CaseNumber, CaseOwner

或者您可以使用PIVOT

with cte as
(
 Select a.CaseNumber, CaseOwner, type, detail
    From Case a
    Join CaseDetails b on a.CaseNumber = b.CaseNumber
    group by a.CaseNumber, CaseOwner
)
select casenumber, caseowner,pv.* 
from cte pivot(max(detail) for type in (1,2,3)) as pv

答案 1 :(得分:1)

您可以使用条件聚合:

Select c.CaseNumber, c.CaseOwner,
       max(case when cd.type = 1 then cd.Detail end) as detail_1,
       max(case when cd.type = 2 then cd.Detail end) as detail_2,
       max(case when cd.type = 3 then cd.Detail end) as detail_3
From Case c Join
     CaseDetails cd
     on c.CaseNumber = cd.CaseNumber
group by c.CaseNumber, c.CaseOwner;

编辑:

您也可以使用outer apply进行此操作:

select c.*, cd.*
from case c outer apply
     (select max(case when cd.type = 1 then cd.Detail end) as detail_1,
             max(case when cd.type = 2 then cd.Detail end) as detail_2,
             max(case when cd.type = 3 then cd.Detail end) as detail_3
      from CaseDetails cd
      where c.CaseNumber = cd.CaseNumber
     ) cd;

答案 2 :(得分:0)

尝试:

Select a.CaseNumber, a.CaseOwner, 
min(CASE WHEN b.Type = 1 IS NOT NULL THEN b.Detail  END) as 'Detail1',
min(CASE WHEN b.Type = 2 IS NOT NULL THEN b.Detail END) as 'Detail2',
min(CASE WHEN b.Type = 3 IS NOT NULL THEN b.Detail END) as 'Detail3'
From Case a
Join CaseDetails b
on a.CaseNumber = b.CaseNumber
GROUP BY a.CaseNumber, a.CaseOwner

答案 3 :(得分:0)

使用枢轴

select  CaseNumber,CaseOwner,
 [1] as detail1, 
 [2] as detail2 ,
 [3] as detail3
 from 
(select c1.CaseNumber,c1.CaseOwner,c2.Detail,c2.Type
    From Case c1
    Join CaseDetails c2
    on c1.CaseNumber = c2.CaseNumber
) src
PIVOT
(
max(Detail) for Type in ([1],[2],[3])
) pvt