У меня есть эта таблица:
+----------+--------+-----+--------+--------+
| personId | Name | Age | height | weight |
+----------+--------+-----+--------+--------+
| 1 | Aritra | 20 | 5.6 | 56 |
| 1 | Aritra | 30 | 5.6 | 76 |
+----------+--------+-----+--------+--------+
Но нужно, чтобы результат был следующим:
+---------------+----------+--------------+----------+
| AttributeName | personId | Presentvalue | OldValue |
+---------------+----------+--------------+----------+
| Height | 1 | 5.6 | 5.6 |
| weight | 1 | 76 | 56 |
+---------------+----------+--------------+----------+
Я попытался это с помощью шарнира, но я столкнулся с проблемой. Как я могу это сделать, используя или не используя опорную точку?
Спасибо.
union all
вероятно, самый простой метод:
select 'Height' as attributeName, personId,
(case when age = 30 then height end) as newValue,
(case when age = 20 then height end) as oldValue
from t
union all
select 'Weight' as attributeName, personId,
(case when age = 30 then weight end) as newValue,
(case when age = 20 then weight end) as oldValue;