1

I have a employee table with employee ID and salary column but i want transpose of that column but without using pivot keyword.

original table

ID Salary

1   10000               
2   20000               
3   30000               
4   40000               
5   50000               

after select query output like =>>

ID      1       2       3       4       5
------  -----   -----   -----   -----   -----
Salary  10000   20000   30000   40000   50000

enter image description here

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
shubham
  • 113
  • 11
  • See here: https://modern-sql.com/use-case/pivot – Markus Winand Oct 04 '19 at 11:01
  • 1
    Why without using `PIVOT`? That's exactly what it was designed for. Its like saying I want to store some characters on a computer but I'm not allowed to use a string data type; if you have a tool that's right for the job then you should be using it. – MT0 Oct 04 '19 at 11:55

1 Answers1

0

You can use conditional aggregation:

select 'Salary' as id,
       max(case when id = 1 then salary end) as salary_1,
       max(case when id = 2 then salary end) as salary_2,
       max(case when id = 3 then salary end) as salary_3,
       max(case when id = 4 then salary end) as salary_4,
       max(case when id = 5 then salary end) as salary_5
from t;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786