我有一张桌子
id employee leave_days leave_type type
1 ABC 10 sick remove
2 ABC 20 sick add
3 ABC 15 Annual remove
4 ABC 50 Annual add
5 XYZ 10 sick remove
6 XYZ 20 sick add
7 XYZ 15 Annual remove
8 XYZ 50 Annual add
从上表中我将按名称 leave_type 分组,然后我将合并行,输出应如下所示。
我必须按列名 leave_type 进行分组,然后添加名为 leave_allocated 的新列。在 leave_allocated 列中,只有值添加的类型列才会出现。
id employee leave_days leave_type leave_allocated
1 ABC 10 sick 20
2 ABC 15 Annual 50
3 XYZ 10 sick 20
4 XYZ 15 Annual 50
我尝试使用子查询我无法将内部查询与外部查询匹配。
答案 0 :(得分:2)
这应该有帮助
SELECT id,
employee,
leave_dates,
leave type,
(SELECT leave_days
FROM TABLE t2
WHERE t2.id = t1.id
AND t2.type = 'add'
) leave_allocated
FROM TABLE t1
WHERE t1.type = 'remove'