合并为一个字段吗?
select a.*,(select count(*) from hp1 where id=a.id and column1<=a.column1) IDX into #t1 from hp1 a
select a.id,
(rtrim(convert(varchar(10),sum(case when idx=1 then column1 else '' end)))+','+
rtrim(convert(varchar(10),sum(case when idx=2 then column1 else '' end))) +','+
rtrim(convert(varchar(10),sum(case when idx=3 then column1 else '' end)))) 'new column'
from #t1 a
group by id
select a.*,(select count(*) from hp1 where id=a.id and column1<=a.column1) IDX into #t1 from hp1 a
select a.id,
sum(case when idx=1 then column1 else '' end) '1',
sum(case when idx=2 then column1 else '' end) '2',
sum(case when idx=3 then column1 else '' end) '3'
from #t1 a
group by id
如果不想用游标,可以这样试试
SELECT A.id,
newcolumn=B.column+' '+C.column+' '+D.column
FROM (SELECT DISTINCT id FROM yourtable) AS A,
yourtable AS B,
yourtable AS C,
yourtable AS D
WHERE A.id=B.id
and A.id=C.id
and A.id=D.id
and C.column<D.column
and C.column>B.column
如果不想用游标,可以这样试试
SELECT A.id,
newcolumn=B.column+' '+C.column+' '+D.column
FROM (SELECT DISTINCT id FROM yourtable) AS A,
yourtable AS B,
yourtable AS C,
yourtable AS D
WHERE A.id=B.id
and A.id=C.id
and A.id=D.id
and C.column<D.column
and C.column>B.column
select a.*,(select count(*) from hp1 where id=a.id and column1<=a.column1) IDX into #t1 from hp1 a
select a.id,
sum(case when idx=1 then column1 else '' end) '1',
sum(case when idx=2 then column1 else '' end) '2',
sum(case when idx=3 then column1 else '' end) '3'
from #t1 a
group by id
select a.*,(select count(*) from hp1 where id=a.id and column1<=a.column1) IDX into #t1 from hp1 a
select a.id,
sum(case when idx=1 then column1 else '' end) '1',
sum(case when idx=2 then column1 else '' end) '2',
sum(case when idx=3 then column1 else '' end) '3'
from #t1 a
group by id
SELECT * INTO #T1 FROM 表1
INSERT #T SELECT ID,MIN(column1) FROM #T1 GROUP BY ID
DELETE #T1 FROM #T1 A
WHERE column1=(SELECT MIN(column1) FROM #T1 WHERE ID=A.ID)
WHILE EXISTS (SELECT * FROM #T1)
BEGIN
UPDATE #T
SET [new column]=[new column]+' ' +B.column1
FROM #T A,#T1 B
WHERE A.ID=B.ID
AND B.column1=(SELECT MIN(column1) FROM #T1 WHERE ID=B.ID)
DELETE #T1 FROM #T1 A
WHERE column1=(SELECT MIN(column1) FROM #T1 WHERE ID=A.ID)
END
select a.*,(select count(*) from table1 where id=a.id and column1<=a.column1) IDX into #t1 from table1 a
select distinct a.id,
( select (case when idx=1 then column1 else '' end) from #t1 where id=a.id) '1',
( select (case when idx=2 then column1 else '' end) from #t1 where id=a.id) '2',
( select (case when idx=3 then column1 else '' end) from #t1 where id=a.id) '3'
from #t1 a