Oracle学习总结(4)——MySql、SqlServer、Oracle数据库行转列大全

MySql行转列

以id分组,把name字段的值打印在一行,逗号分隔(默认)

select CustomerDrugCode,group_concat(AuditItemName) from noaudit_drug group by CustomerDrugCode;

以id分组,把name字段的值打印在一行,分号分隔

select CustomerDrugCode,group_concat(AuditItemName separator ';') from noaudit_drug group by CustomerDrugCode;

以id分组,把去冗余的name字段的值打印在一行

select CustomerDrugCode,group_concat(distinct AuditItemName) from noaudit_drug group by CustomerDrugCode;

以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序

select CustomerDrugCode,group_concat(AuditItemName order by AuditItemName desc) from noaudit_drug group by CustomerDrugCode;

SqlServer行转列



SELECT 药品名称,药品ID,类型s FROM (

SELECT 药品名称,药品ID,

(SELECT 类型+',' FROM 开药量单位$ WHERE 药品ID=A.药品ID FOR XML PATH('')) AS 类型s

FROM 开药量单位$ A

GROUP BY 药品名称,药品ID

) B

Oracle行转列

select namecn,wm_concat(dname) name from Table GROUP BY namecn

上一篇:Linux 积累


下一篇:注册界面设计及实现之(三)SharedPerferences实现数据暂存