-1

I have a few rows in a column as below:

Country
----------
 USA
 UK
 Singapore

Now, I want to have result as below exactly:

First_Country |  Second_Country |  Third_Country
--------------+-----------------+--------------
 USA          |   UK            |   Singapore
 NULL         |   USA           |   UK

How to do? I tried Pivot or Unpivot no luck.

Ullas
  • 11,450
  • 4
  • 33
  • 50
Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • Please provide output sample in exact tabular form you want it to be. – Kasim Husaini Sep 18 '14 at 05:16
  • @KasimHusaini: Thanks, now it is in correct format. Please help. I am using 2008 Management Studio. Is there simple way to do in 2012? – Jasmine Sep 18 '14 at 05:44
  • How do you determine which row gets grouped with which other rows? There needs to be another column to determine the groups of rows to pivot. Grouping without it would probably be possible but totally arbitrary. – Andriy M Sep 19 '14 at 14:43

1 Answers1

0

You can use the below Query to achieve this:

select  GROUP_CONCAT(<Column_Name> SEPARATOR ' ') from <Table_Name>;

Where, Column_Name - Column you would want to concat into single result Table_Name - Table which has the above column.

  • Janakiraman, thank you, however, it says GROUP_CONCAT is not a recognized function. I am using 2008 Management Studio. Please help. Is this dependent on Version? – Jasmine Sep 18 '14 at 05:42
  • The Above query is specific to MySQL. For SQL Server you can use something like this:SELECT STUFF( (SELECT ',' + Column_Name FROM Table_Name FOR XML PATH ('')) , 1, 1, '') – Janakiraman TK Sep 18 '14 at 06:43
  • Janakiraman, thank you, this query looks better. But, is it possible to have it in different column as I given the output above in my question? 2nd Row should have NULL in the first column – Jasmine Sep 18 '14 at 09:30
  • on what basis the second row has null value? – Janakiraman TK Sep 18 '14 at 11:10