简体   繁体   English

在SQL中将一栏变成字符串

[英]Turning one column into a string in SQL

I have a table in SQL that look like this: 我在SQL中有一张表,如下所示:

IMG

The issue is that I would like it to be displayed in this fashion: 问题是我希望以这种方式显示它:

Medication Review | Allergies, CAM, Diet Supp, 

Having one presenting problem and all of the secondary problems in a single row instead of the way it is now. 单行显示一个问题和所有次要问题,而不是现在的样子。

Any help would be awesome! 任何帮助都是极好的! Thanks! 谢谢!

If you are using SQL Server, you can do this: 如果使用的是SQL Server,则可以执行以下操作:

SELECT DISTINCT t1.presenting_problem,
  STUFF(
         (SELECT ', ' + t2.secondary_problem
          FROM tableName t2
          where t1.presenting_problem = t1.presenting_problem
          FOR XML PATH (''))
          , 1, 1, '')  AS secondary_problems
from tableName t1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM