作者popcorny (毕业了..@@")
看板Database
标题Re: [SQL ] 列出一个栏位中开始变化的前後值
时间Sat Apr 30 23:07:58 2016
※ 引述《Schematic (小小宝的妈)》之铭言:
: 预期结果,员工1在99年从B单位换到A单位
: id year unit
: -----------------------
: 1 99 A
: 1 98 B
: 3 95 C
: 3 94 B
: 4 97 A
: 4 96 B
: 谢谢
你的预期结果其实不容易实现,
如果换个题目呢?
呈现员工各个单位的在职年度区间
select
id,
unit,
min(year) as fromYear,
max(year) as toYear
from Emp
group by id, unit
order by id, fromYear;
输出
ID Unit fromYear toYeader
--------------------------
1 B 96 98
1 A 99 99
2 B 97 99
3 B 94 94
3 C 95 97
4 B 95 96
4 A 97 97
这样可以看出员工1
B单位 96-98
A单位 99-99
个人认为这样也比较好处理
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.171.2.36
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1462028881.A.624.html