铭毅小子

数据库行转列、列转行、字段拼接

列转行

select prd_no_chg_y,

SUBSTRING(t.prd_no_chg_y, number ,CHARINDEX(';',t.prd_no_chg_y+';',number)-number) prd_no_chg

from tb t,master..spt_values s

where s.number >=1

and s.type = 'P'

and SUBSTRING(';'+t.prd_no_chg_y,s.number,1) = ';'

 

行转列

select * into #b from #a a
PIVOT
(
SUM(summinute) FOR
a.fday IN (b1,b2)
) AS T

拼接stuff写法

SELECT id, 
                      value = stuff
                          ((SELECT     ',' + value
                              FROM         tb AS t
                              WHERE     t .id = tb.id FOR xml path('')), 1, 1, '')
FROM         tb
GROUP BY id

posted on 2020-11-17 22:19  铭毅小子  阅读(224)  评论(0编辑  收藏  举报

导航