Sql 将多个行合并为具有相同ID的单个行
当我对(调查的)两列执行查询时,会出现问题,因为我希望输出点为单个ID,而不是查询将ID提供的每个响应的输出分开 原始查询Sql 将多个行合并为具有相同ID的单个行,sql,rows,Sql,Rows,当我对(调查的)两列执行查询时,会出现问题,因为我希望输出点为单个ID,而不是查询将ID提供的每个响应的输出分开 原始查询 select distinct _PanelistaId_, (case when cancl_seg_vida_mr = '4' then '1' end) as cancl_seg_vida_mr, (case when prop_seg_med_mr = '5' then '1' end) as prop_seg_med_mr FROM [NQ_Mexico].[db
select distinct _PanelistaId_,
(case when cancl_seg_vida_mr = '4' then '1' end) as cancl_seg_vida_mr,
(case when prop_seg_med_mr = '5' then '1' end) as prop_seg_med_mr
FROM [NQ_Mexico].[dbo].[tabla_sr_mr_values]
where _PanelistaId_ = '100177354'
group by _PanelistaId_, cancl_seg_vida_mr, prop_seg_med_mr
输出是:
_PanelistaId_ cancl_seg_vida_mr prop_seg_med_mr
100177354 NULL NULL
100177354 NULL 1
100177354 1 NULL
但我想要的结果是:
_PanelistaId_ cancl_seg_vida_mr prop_seg_med_mr
100177354 1 1
使用聚合:
select _PanelistaId_,
max(case when cancl_seg_vida_mr = '4' then '1' end) as cancl_seg_vida_mr,
max(case when prop_seg_med_mr = '5' then '1' end) as prop_seg_med_mr
from [NQ_Mexico].[dbo].[tabla_sr_mr_values]
where _PanelistaId_ = '100177354'
group by _PanelistaId_;