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