0

how can I convert rows of my table as column value for eg I have a table a

emp id
1
2
3
4

and I want my output as

1   2   3   4

i am also using pivot in oracle and crosstab in postgres but not able to get desired solution as shown above.

  • 1
    "*not able to get desired solution*" is not an acceptable problem description. –  Dec 18 '13 at 08:11
  • Maybe this can help http://stackoverflow.com/questions/15491661/dynamic-pivot-in-oracle-sql or this http://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION_ID:2648932900346131308 – A.B.Cade Dec 18 '13 at 08:34
  • possible duplicate of [Oracle Rows to Column Transformation](http://stackoverflow.com/questions/9582218/oracle-rows-to-column-transformation) – John Doyle Dec 18 '13 at 13:20

3 Answers3

1

Check the listagg function. Note you need at least Oracle 11 for this.

Guntram Blohm
  • 9,667
  • 2
  • 24
  • 31
0

Use this script and change it to your values to get desired results

    SELECT * FROM (SELECT emp_id, emp_points
    FROM   emp_data)
    PIVOT  (
                SUM(emp_points) AS sum_emp_points 
                FOR (emp_id) IN (1 AS [1], 2 AS [2],3 AS [3],4 AS [4])
           );
Anoop
  • 379
  • 4
  • 14
  • This doesn't look like a general solution, but specific to the data described in the question. It's better to make solutions that generally apply to similar situations without data type or length limits. – ADTC Dec 18 '13 at 08:24
  • I can provide specific answer only to a specific question with some clear explanation on data. – Anoop Dec 18 '13 at 09:02
0
   select * from (select empno from t7) pivot(min(empno) for empno in (1,2,3,4));
Sai
  • 659
  • 4
  • 12
  • 21
  • select * from (select emp_id from your_table) pivot(min(emp_id) for emp_id in (1,2,3,4)); – Sai Dec 18 '13 at 17:39