问题
I've again a basic question :
If I do :
SELECT DISTINCT id, date, doctext, docline, from documentation where date in (02/14/2017)
doctext is a char(80) and I cannot change it. The problem of this columns is the size, I cannot save a value > 80 characters, If the doc is > 80 char, it will save two lines in SQL and upgrade the docline
So my result is, for example :
0 2017-02-14 this is a basic test to show you the result 0
1 2017-02-14 this is a new basic test to show you the result 0
2 2017-02-14 this is a long basic test to show you the result 0
2 2017-02-14 when the documentation have multiple lines 1
What I'm trying to do is to concatenate doctext if the result have multiple line with the same id So the result should be :
0 2017-02-14 this is a basic test to show you the result
1 2017-02-14 this is a new basic test to show you the result
2 2017-02-14 this is a long basic test to show you the result when the documentation have multiple lines 1
Is it possible to concatenate a column on one line depends on a id ? I'm trying with CASE like :
CASE WHEN docline > 0 THEN DOCTEXT ...
I don't know how can I spectify that I would like the next DOCTEXT
Thanks,
回答1:
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
回答2:
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 DocTextfrom MasterTable mtfull outer join DetailsTable dton 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 ?--------------------------------------------
来源:https://stackoverflow.com/questions/42237624/concatenate-a-column-on-one-line-depends-on-a-id