1

In SQL when i execute a query with projection of one column from a "table A" am getting results set in three rows of that column , I need it in one row side by side.

Example

select rank from table A where name="RAMESH"

I have 1,2,3 ranks on the name "ramesh" am getting result set as

RANK
----
1
2
3

but I need like this

RANK(1)  RANK(2)   RANK(3)
-------   -----  --------
1           2       3

Please help me out....

Thanks in Advance

Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • 1
    Go through this post: http://stackoverflow.com/questions/14834290/mysql-query-to-dynamically-convert-rows-to-columns you will get a good idea. – Neels Jun 05 '14 at 07:36
  • Don't do it on database level, do it on application level. – fancyPants Jun 05 '14 at 07:40
  • 1
    I tried by rotating the monitor. But now text is seen down to up instead of left to right. How can I fix this? – Ravinder Reddy Jun 05 '14 at 07:44
  • possible duplicate of [MySQL pivot table](http://stackoverflow.com/questions/7674786/mysql-pivot-table) –  Aug 15 '14 at 07:38

1 Answers1

-1

Well this will probably work

WITH x AS (SELECT rank FROM table A WHERE name="RAMESH")
SELECT * 
FROM 
    (SELECT * FROM x LIMIT 0, 1)as a,
    (SELECT * FROM x LIMIT 1, 1)as b,
    (SELECT * FROM x LIMIT 2, 1)as c
LIMIT 1;

You join the first column with the second and then the third

GramThanos
  • 3,572
  • 1
  • 22
  • 34
  • 1
    MySQL does not support common table expressions, and `LIMIT` without order by is not deterministic. – GarethD Jun 05 '14 at 07:56
  • I used limit to make it faster in performance. Without LIMIT it will still work but you will have to ignore all the other rows except the first. As you can't use with you can make multiple selects. – GramThanos Jun 05 '14 at 08:00