将一个 table 复制到另一个加上依赖于另一列的一列
Copying one table to another plus one column dependant of another column
NAME 和 VER 是主键。
我不会写下我想做的事情,而是会画画。
Table 来源:
NAME | SOMETHING1 | VER | SOMETHING2 | MODDATIME
john | bob | 1 | doe | 18-AUG-14 10.16.08.695221100 AM
john | bob | 2 | doe | 19-AUG-14 11.01.42.109826000 AM
john | bob | 3 | doe | 09-SEP-14 10.14.15.929342100 AM
john | bob | 4 | doe | 10-SEP-14 10.14.15.929342100 AM
Table 目标:
NAME | SOMETHING1 | VER | SOMETHING2 | MODDATIME | DATEUP
john | bob | 1 | doe | 18-AUG-14 10.16.08.695221100 AM | 19-AUG-14 11.01.42.109826000 AM
john | bob | 2 | doe | 19-AUG-14 11.01.42.109826000 AM | 09-SEP-14 10.14.15.929342100 AM
john | bob | 3 | doe | 09-SEP-14 10.14.15.929342100 AM | 10-SEP-14 10.14.15.929342100 AM
john | bob | 4 | doe | 10-SEP-14 10.14.15.929342100 AM | 31-DEC-99 0.0.0.0 AM
谢谢。
用相同的Table做一个OUTER JOIN
得到结果
SELECT a.NAME,
a.something,
a.VER,
a.something,
a.MODDATIME,
Ifnull(b.MODDATIME,'31-DEC-99 0.0.0.0 AM') AS DateUP
FROM Yourtable a
LEFT OUTER JOIN Yourtable b
ON a.NAME = b.NAME
AND a.VER = b.VER - 1
NAME 和 VER 是主键。 我不会写下我想做的事情,而是会画画。
Table 来源:
NAME | SOMETHING1 | VER | SOMETHING2 | MODDATIME
john | bob | 1 | doe | 18-AUG-14 10.16.08.695221100 AM
john | bob | 2 | doe | 19-AUG-14 11.01.42.109826000 AM
john | bob | 3 | doe | 09-SEP-14 10.14.15.929342100 AM
john | bob | 4 | doe | 10-SEP-14 10.14.15.929342100 AM
Table 目标:
NAME | SOMETHING1 | VER | SOMETHING2 | MODDATIME | DATEUP
john | bob | 1 | doe | 18-AUG-14 10.16.08.695221100 AM | 19-AUG-14 11.01.42.109826000 AM
john | bob | 2 | doe | 19-AUG-14 11.01.42.109826000 AM | 09-SEP-14 10.14.15.929342100 AM
john | bob | 3 | doe | 09-SEP-14 10.14.15.929342100 AM | 10-SEP-14 10.14.15.929342100 AM
john | bob | 4 | doe | 10-SEP-14 10.14.15.929342100 AM | 31-DEC-99 0.0.0.0 AM
谢谢。
用相同的Table做一个OUTER JOIN
得到结果
SELECT a.NAME,
a.something,
a.VER,
a.something,
a.MODDATIME,
Ifnull(b.MODDATIME,'31-DEC-99 0.0.0.0 AM') AS DateUP
FROM Yourtable a
LEFT OUTER JOIN Yourtable b
ON a.NAME = b.NAME
AND a.VER = b.VER - 1