SQL join column within 1 table

I'm busy with a script to display transactions.

I want to write a SQL query that sets everything on one line.

Here is the SQL:

SELECT transactienummer, code
FROM gb_kaarten 
ORDER BY transactienummer DESC
LIMIT 4

      

this is the result:

transactienummer code
43141            1600
43141            4410
43141            1513
43141            1514

      

I just want to have a line that looks like this:

transactienummer code
43141            1600        4410         1513         1514

      

I've tried some joins, but I don't seem to get it.

Thanks in forehand for the help

+3


source to share


3 answers


I assume you are using mysql, you should do something like this:

SELECT transactienummer, GROUP_CONCAT(string SEPARATOR ' ') 
FROM gb_kaarten
GROUP BY code;

      



Note that you will have the entire result in one field divided by "

+4


source


DECLARE @col NVARCHAR(MAX);
SELECT @col = COALESCE(@col + char, '') +Code 
  FROM gb_kaarten

select transactienummer,@col 
from gb_kaarten
group by transactienummer

      



NOTE. It will only work for one common type

0


source


You can find it on SO Link below

fooobar.com/questions/2975 / ...

In your case find trying this

SELECT distinct [transactienummer]
,(STUFF((SELECT CAST('   ' + Code AS VARCHAR(MAX)) 
     FROM Table
     WHERE (transactienummer= Table.transactienummer) 
     FOR XML PATH ('')), 1, 2, '')) AS Code
FROM Table

      

Find a solution in the Fiddle http://sqlfiddle.com/#!6/f8339/1

0


source







All Articles