sql查询符合条件的记录为多条时,如果用循环语句一条一条的取出字段值

2024-12-02 21:13:27
推荐回答(1个)
回答1:

;with roy as
(select 字段1,字段2,row=row_number()over(partition by 字段1 order by 字段老耐敏1) from Tab)
,roy2 as
(select 字段1,cast(字段2 as nvarchar(100))字段2,row from Roy where row=1
union all
select a.字段1,cast(b.字段2+','+a.字段2 as nvarchar(100)),a.row from Roy a join roy2 b on a.字段1=b.字侍枝段亩差1 and a.row=b.row+1)

select 字段1,字段2 from roy2 a where row=(select max(row) from roy where 字段1=a.字段1) order by 字段1 option (MAXRECURSION 0)