Oracle-PS常用的SQL语句

PS常用的SQL语句

查询某部门的所有子部门

1
2
3
4
5
--包含所有的子部门
select b.deptid,b.* from Ps_c_Org_Path_Tbl b where INSTR (b.c_org_path2, '100000002' ) > 0 ;
/*
c_Org_Path_Tbl表是部门路径表,包含了部门的所有子部门ID和路径。
*/

查询当前月份的第一天

1
2
3
4
5
6
7
--某个月份的第一天
select trunc(add_months(last_day(sysdate), -1) + 1) from dual
/*
函数:last_day(sysdate)-获取当前日期的最后一天;
函数:add_months(last_day(sysdate), -1)-获取前一个月的最后一天;
函数:trunc(add_months(last_day(sysdate), -1) + 1)-前一个月最后一天加一天以后,取整部分;
*/

列出某段时间的每一天

1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT TO_CHAR(TRUNC(SYSDATE) - ROWNUM + 1, 'YYYY-MM-DD') AS TIME_DATE FROM DUAL CONNECT BY ROWNUM <= 10;
/* 查询结果:
2023-05-06
2023-05-05
2023-05-04
2023-05-03
2023-05-02
2023-05-01
2023-04-30
2023-04-29
2023-04-28
2023-04-27
*/

查询每条职务数据的前一条数据,生成链路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
select j.emplid   as 员工ID,
j.empl_rcd as 员工记录,
j.effdt as 生效日期,
p.jobcode as 变化前jobcode,
j.jobcode as 变化后jobcode,
p.deptid as 变化前部门,
j.deptid as 变化后部门
from sysadm.ps_job j,
sysadm.ps_job p,
(select v.emplid,
v.empl_rcd,
v.effdt,
v.effseq,
lead(v.effdt) over(partition BY v.emplid, v.empl_rcd ORDER BY v.emplid, v.empl_rcd, v.effdt desc, v.effseq desc) AS pre_effdt,
lead(v.effseq) over(partition BY v.emplid, v.empl_rcd ORDER BY v.emplid, v.empl_rcd, v.effdt desc, v.effseq desc) AS pre_effseq
from sysadm.ps_job v
where v.empl_rcd = 0) a
where j.emplid = a.emplid
and j.empl_rcd = a.empl_rcd
and j.effdt = a.effdt
and j.effseq = a.effseq
and p.emplid = a.emplid
and p.empl_rcd = a.empl_rcd
and p.effdt = a.pre_effdt
and p.effseq = a.pre_effseq
and j.jobcode <> p.jobcode
and j.effdt >= TO_DATE('2020-01-01', 'YYYY-MM-DD');