仅包含值的单行

时间:2016-06-22 00:24:31

标签: sql sql-server

如何只使用值获得单行,我不希望看到NULL值。我知道我可以从这个查询中创建一个Temp表,然后在Temp表上选择MAX,但是想知道是否有办法在这个查询中执行它。

Select VisitIDCode
,CASE WHEN (LeftJustifiedLabel) = 'Additional Intervention Information' Then (ValueText) Else Null End As 'Additional Intervention Information'
,CASE WHEN (LeftJustifiedLabel) = 'American Cancer Society Comment:' Then (ValueText) Else Null End As 'American Cancer Society Comment:'
,CASE WHEN (LeftJustifiedLabel) = 'Intevention' Then (ValueText) Else Null End As 'Intevention'
From #AssessmentFS
WHERE VisitIDCode = 123

这是我上面查询的当前最终结果,我只添加了一个Case字段,因为它对其他字段来说很长。

VisitIDCode  Additional Intervention Information  
123          Faxed referral to 877-428-2862
123          NULL
123          NULL      

1 个答案:

答案 0 :(得分:1)

您可以在同一查询中汇总(使用maxmin等)。

select 
VisitIDCode
,MAX(CASE WHEN LeftJustifiedLabel = 'Additional Intervention Information' Then ValueText End) As 'Additional Intervention Information'
,MAX(CASE WHEN LeftJustifiedLabel = 'American Cancer Society Comment:' Then ValueText End) As 'American Cancer Society Comment:'
,MAX(CASE WHEN LeftJustifiedLabel = 'Intevention' Then ValueText End) As 'Intevention'
From #AssessmentFS
group by VisitIDCode