Это очень необычное требование, и я полагаю, что вам на самом деле не нужно указывать значения в том порядке, в котором вы запрашиваете, а отвечать на вопрос в виде вопроса:
-- Build data
declare @t table(ID int,Col1 varchar(10),Col2 varchar(10),Col3 varchar(10),Col4 varchar(10),Col5 varchar(10));
insert into @t values
(1,'MALE','MALE','FEMALE',null,null)
,(2,'FEMALE','MALE',null,null,null)
,(3,'FEMALE',null,null,null,null)
,(4,'MALE','OTHER','FEMALE','FEMALE',null)
,(5,'MALE','OTHER','FEMALE','MALE','FEMALE')
;
-- Query
with d as
( -- Manually unpivot the 5 Cols and add a row number
select 1 as r
,ID
,Col1 as Col
from @t
union all
select 2
,ID
,Col2 as Col
from @t
union all
select 3
,ID
,Col3 as Col
from @t
union all
select 4
,ID
,Col4 as Col
from @t
union all
select 5
,ID
,Col5 as Col
from @t
)
,c as
( -- Replace Col values for presentation and add in aggregated row numbers, ranks and counts for each value
select ID
,case Col when 'MALE' then 'Male'
when 'FEMALE' then 'Female'
when 'OTHER' then 'Other'
end as Col
,row_number() over (partition by ID order by r) as rn
,dense_rank() over (partition by ID order by r) as drk
,rank() over (partition by ID, Col order by r) as rk
,count(Col) over (partition by ID, Col) as c
from d
where Col is not null
)
,o as
( -- Filter rows down to just the first instance of that Col value to get the presentation order
select ID
,row_number() over (partition by ID order by rn) as o
,cast(c as varchar(10)) + ' ' + Col as c
from c
where rk = 1
)
-- Collate the data per presentation rules
select o.ID
,o.c
+ isnull(case when o3.ID is null
then ' and ' + o2.c
else ', ' + o2.c + ' and ' + o3.c
end
,'') as Remarks
from o
left join o as o2
on o.ID = o2.ID
and o2.o = 2
left join o as o3
on o.ID = o3.ID
and o3.o = 3
where o.o = 1
order by o.ID;
Выход:
+----+------------------------------+
| ID | Remarks |
+----+------------------------------+
| 1 | 2 Male and 1 Female |
| 2 | 1 Female and 1 Male |
| 3 | 1 Female |
| 4 | 1 Male, 1 Other and 2 Female |
| 5 | 2 Male, 1 Other and 2 Female |
+----+------------------------------+