我有这两个表:
+-------------+--------+ +----+-------+
| employee_id | val_id | | id | value |
+-------------+--------+ +----+-------+
| 123 | 1 | | 1 | A |
+-------------+--------+ +----+-------+
| 123 | 2 | | 2 | B |
+-------------+--------+ +----+-------+
| 123 | 3 | | 3 | C |
+-------------+--------+ +----+-------+
预期结果
+-------------+-------+-------+-------+
| employee_id | val_1 | val_2 | val_3 | ...
+-------------+-------+-------+-------+
| 123 | A | B | C |
请你帮帮我
答案 0 :(得分:0)
答案 1 :(得分:0)
使用join和pivot。这将提供所需的结果
SELECT * FROM(
SELECT employeeID,'val_'+CAST(a.val_id AS VARCHAR(2)) val_id ,value
FROM table1 a INNER JOIN table2 b ON a.val_id=b.val_id
) AS ss
PIVOT
(
MAX(value)
FOR [val_id] IN ([val_1],[val_2],[val_3])
)AS pivott