concatenate a column on one line depends on a id

一笑奈何 提交于 2019-12-06 12:36:56

Something like this should work

SELECT id, date,MAX(docline),
Ids=Stuff((SELECT ' ' + doctext  FROM documentation  d WHERE d.id=documentation.id
 FOR XML PATH (''))
             , 1, 1, '' )
 from documentation where date in (02/14/2017)
GROUP BY id,date

For storing multiple lines you used the docline column but I didn't use it. if you want you can add it.

I used two tables(MasterTable and DetailsTable)

Master Table:

ID | Date | DocText
-----------------------------
1 2017-01-14 Hello
-----------------------------
2 2017-03-18 Good Bye
------------------------------
3 2017-02-14 Hello Iran
------------------------------
4 2017-02-14 Good Bye Iran
------------------------------

DetailsTable:

ID | DocText
---------------------------
3 How are you ?
---------------------------
4 See you ?
---------------------------

Use Test;
go

select mt.ID,mt.DocText + ' ' + ISNULL(dt.DocText,'') as DocText
from MasterTable mt
full outer join DetailsTable dt
on mt.ID=dt.ID;

It works for multiple lines.

the result:

ID | Date | DocText
--------------------------------------------
1 2017-01-14 Hello
--------------------------------------------
2 2017-03-18 Good Bye
--------------------------------------------
3 2017-02-14 Hello Iran How are you ?
--------------------------------------------
4 2017-02-14 Good Bye Iran See you ?
--------------------------------------------

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!