这是我的表:
+----+-------+-----------+-------+
| id | Name | Parameter | Value |
+----+-------+-----------+-------+
| 1 | Andy | height | 1.85 |
| 2 | Igal | weight | 56 |
| 3 | Yossi | age | 28 |
| 4 | David | weight | |
| 5 | Igal | height | 1.9 |
| 6 | Andy | age | 25 |
+----+-------+-----------+-------+
这是我的疑问:
SELECT
Name,
CASE WHEN (Paramter='age') THEN Value ELSE 0 END AS AGE,
CASE WHEN (Paramter='height') THEN Value ELSE 0 END AS HEIGHT,
CASE WHEN (Paramter='weight') THEN Value ELSE 0 END AS WEIGHT
FROM
table
结果:
+-------+-----+--------+--------+
| Name | AGE | HEIGHT | WEIGTH |
+-------+-----+--------+--------+
| Andy | 0 | 1.85 | 0 |
| Igal | 0 | 0 | 56 |
| Yossi | 28 | 0 | 0 |
| David | 0 | 0 | |
| Igal | 0 | 1.9 | 0 |
| Andy | 25 | 0 | 0 |
+-------+-----+--------+--------+
我也试过按[名字]分组,但没有成功。
如何为每个人(姓名)显示一个行中的所有数据?
通缉表:
+------+-----+--------+--------+
| Name | AGE | HEIGHT | WEIGTH |
+------+-----+--------+--------+
| Igal | 0 | 1.9 | 56 |
| Andy | 25 | 1.85 | 0 |
+------+-----+--------+--------+
答案 0 :(得分:0)
你想在那里进行一些聚合:
SELECT Name,
MAX(CASE WHEN Parameter = 'age' THEN Value END) AS AGE,
MAX(CASE WHEN Parameter = 'height' THEN Value END) AS HEIGHT,
MAX(CASE WHEN Parameter = 'weight' THEN Value END) AS WEIGHT
FROM table
GROUP BY Name;
我想指出缺少ELSE 0
。如果没有数据,我认为没有理由为这三列插入0
值。