2

I have a table like

Table1 Image

and

i want that table to be converted to

Table2 Image

Using TeraData Query

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Kalyan Ganta
  • 45
  • 1
  • 8
  • hey For **TeraData** follow this if it helps https://forgetcode.com/teradata/1294-converting-columns-into-rows please check link SQL Server https://stackoverflow.com/questions/15745042/efficiently-convert-rows-to-columns-in-sql-server?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Hardik Masalawala Apr 23 '18 at 10:30
  • Have a look at this thread: https://stackoverflow.com/q/49917409/2527905 – dnoeth Apr 23 '18 at 11:23

1 Answers1

1

If you know the list of values, you can use conditional aggregation:

select max(case when name = 'AA' then value end) as aa,
       max(case when name = 'BB' then value end) as bb,
       max(case when name = 'CC' then value end) as cc,
       . . .
from t;

If you don't know the list of values, then you cannot do this with a simple SQL query.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Hi thanks for the answer it works when column names are static but in my case Column names and their values changes dynamically so instead of hard coding the case is it possible to make it work for dynamically generated table? – Kalyan Ganta Apr 23 '18 at 11:04
  • Dynamically generated SQL is allowed only in stored procedures. You can compile any query as string and than execute it. – Alex Apr 24 '18 at 09:30