原資料
結果資料庫
通過for xml path把skill按-拼接在一起(-纏繞-寄生-蛛網束縛),然後使用stuff函式把第乙個-符號去掉。
sql code
select a.*
into #result
from
( select '唐三' as name,'纏繞' as skill
union all
select '唐三' as name,'寄生' as skill
union all
select '唐三' as name,'蛛網束縛' as skill
union all
select '小舞' as name,'爆殺八段摔' as skill
union all
select '小舞' as name,'無敵金身' as skill
) aselect name,
stuff(
(select '-'+ a1.skill from #result a1 where a1.name=a.name for xml path(''))
,1,1,''
) as skill
from #result a
group by name
order by name
drop table #result
原資料
結果資料庫
通過convert(xml,'' + replace(a.skill, '-', '
') + '
sql code
select a.*
into #result
from
( select '唐三' as name,'纏繞-寄生-蛛網束縛' as skill
union all
select '小舞' as name,'爆殺八段摔-無敵金身' as skill
) aselect a.name,b.skill from
(select a.name,skill=convert(xml,'' + replace(a.skill, '-', '
') + '
')from #result a
)aselect skill = n.v.value('.', 'varchar(100)') from a.skill.nodes('/root/v') n(v)
)bdrop table #result
原資料
結果資料庫
select '唐三' as name,'纏繞-寄生-蛛網束縛' as skill from dual
union all
select '小舞' as name,'爆殺八段摔-無敵金身' as skill from dual
) aconnect by level <= length(a.skill) - length(regexp_replace(a.skill, '-', '')) + 1) order by name;
select routine_name, routine_definition
from information_schema.routines
where routine_definition like '%表名%'
and routine_type='procedure'
SQL 行轉列,列轉行
行列轉換在做報表分析是還是經常會遇到的 行列轉換就是如下圖所示兩種展示形式互相轉換 只是做測試,建表比較隨意 create table student name varchar 20 subject int 10 score int 10 insert into student name,subje...
python 列轉行 SQL 行轉列,列轉行
sql 行轉列,列轉行 行列轉換在做報表分析時還是經常會遇到的,今天就說一下如何實現行列轉換吧。行列轉換就是如下圖所示兩種展示形式的互相轉換 行轉列假如我們有下表 select from student pivot sum score for subject in 語文,數學,英語 通過上面 sql...
sql 行轉列,列轉行整合
原始資料及結構如下 when 語文 then t.score else 0 end 語文,sum case t.subject when 數學 then t.score else 0 end 數學 sum case t.subject when 英語 then t.score else 0 end ...