我必须根据第一列在同一 table 中连接两行
I have to concatenate two rows in the same table based on the first columns
这里我展示的是table
Column_1 Column_2
------------------------------------------------------------------------------
GERD Onset: 3 Days. Pain scale: 9/10.
hyperlipidemia The diabetes started in 2004 and is controlled.
asthma Onset: 6 hours ago. The initial visit date was
headache Onset: 1 Day. The severity of the problem is
hyperlipidemia (comments) This is a test Comment where i can try
asthma (comments) Test Comment for the query
我要的是这个
Column_1 Column_2
------------------------------------------------------------------------------
GERD Onset: 3 Days. Pain scale: 9/10.
hyperlipidemia The diabetes started in 2004 and is controlled. Comment : This is a test Comment where i can try
asthma Onset: 6 hours ago. The initial visit date was
headache Onset: 1 Day. The severity of the problem is. Comment : Test Comment for the query
我的SQL声明:
select a.create_timestamp, a.Column_1 as Name, a.Column_2+b.Column_2 as DetailsAndComment from HOPI_ex_ a, HOPI_ex_ b
where a.Column_1+' (comments)' = b.Column_1
我只得到两行
Gerd 和高脂血症
您只得到两行的原因是您的 where 子句过滤掉了没有相应评论的行。我认为使用左连接应该做你想做的。请试试这个:
select
a.create_timestamp,
a.Column_1 as Name,
case when b.Column_2 is null then a.Column_2 else a.Column_2 + ' Comment: ' + b.Column_2 end as DetailsAndComment
from HOPI_ex_ a
left join HOPI_ex_ b on a.Column_1 + ' (comments)' = b.Column_1
where a.Column_1 not like '%(comments)%'
使用您的示例数据,上述查询的结果将是:
Name DetailsAndComment
----------------- -----------------------------------------
GERD Onset: 3 Days. Pain scale: 9/10.
hyperlipidemia The diabetes started in 2004 and is controlled. Comment: This is a test Comment where i can try
asthma Onset: 6 hours ago. The initial visit date was Comment: Test Comment for the query
headache Onset: 1 Day. The severity of the problem is
这里我展示的是table
Column_1 Column_2
------------------------------------------------------------------------------
GERD Onset: 3 Days. Pain scale: 9/10.
hyperlipidemia The diabetes started in 2004 and is controlled.
asthma Onset: 6 hours ago. The initial visit date was
headache Onset: 1 Day. The severity of the problem is
hyperlipidemia (comments) This is a test Comment where i can try
asthma (comments) Test Comment for the query
我要的是这个
Column_1 Column_2
------------------------------------------------------------------------------
GERD Onset: 3 Days. Pain scale: 9/10.
hyperlipidemia The diabetes started in 2004 and is controlled. Comment : This is a test Comment where i can try
asthma Onset: 6 hours ago. The initial visit date was
headache Onset: 1 Day. The severity of the problem is. Comment : Test Comment for the query
我的SQL声明:
select a.create_timestamp, a.Column_1 as Name, a.Column_2+b.Column_2 as DetailsAndComment from HOPI_ex_ a, HOPI_ex_ b
where a.Column_1+' (comments)' = b.Column_1
我只得到两行 Gerd 和高脂血症
您只得到两行的原因是您的 where 子句过滤掉了没有相应评论的行。我认为使用左连接应该做你想做的。请试试这个:
select
a.create_timestamp,
a.Column_1 as Name,
case when b.Column_2 is null then a.Column_2 else a.Column_2 + ' Comment: ' + b.Column_2 end as DetailsAndComment
from HOPI_ex_ a
left join HOPI_ex_ b on a.Column_1 + ' (comments)' = b.Column_1
where a.Column_1 not like '%(comments)%'
使用您的示例数据,上述查询的结果将是:
Name DetailsAndComment
----------------- -----------------------------------------
GERD Onset: 3 Days. Pain scale: 9/10.
hyperlipidemia The diabetes started in 2004 and is controlled. Comment: This is a test Comment where i can try
asthma Onset: 6 hours ago. The initial visit date was Comment: Test Comment for the query
headache Onset: 1 Day. The severity of the problem is