На которой я почему-то залипаю (при том что знаю решение):

Есть таблица:

create table t
  (id number,
   field_name varchar2(20),
   string_value varchar2(20),
   number_value number);
  
insert into t (id, field_name, string_value, number_value)
  values (1, ‘Name’, ‘John’, null);
insert into t (id, field_name, string_value, number_value)
  values (1, ‘Surname’, ‘Smith’, null); 
insert into t (id, field_name, string_value, number_value)
  values (1, ‘Salary’, null, 100); 
 
insert into t (id, field_name, string_value, number_value)
  values (2, ‘Name’, ‘Bill’, null);
insert into t (id, field_name, string_value, number_value)
  values (2, ‘Surname’, ‘Dow’, null); 
insert into t (id, field_name, string_value, number_value)
  values (2, ‘Salary’, null, 200);   
commit;

надо ее “развернуть” на 90 градусов.

решение простое:

select t.id,
       max(decode(field_name, ‘Name’, string_value)) Name,
       max(decode(field_name, ‘Surname’, string_value)) Surname,
       max(decode(field_name, ‘Salary’, number_value)) Salary
  from t
  group by t.id;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: