我有桌子::
CREATE TABLE emp1
(
eid integer NOT NULL,
ename character varying(20),
sid integer,
ssid integer,
CONSTRAINT pk_eid PRIMARY KEY (eid)
);
CREATE TABLE leave_type
(
eid integer,
lid integer,
lnum integer,
emp_bal integer,
sno serial NOT NULL,
CONSTRAINT pk_sno PRIMARY KEY (sno),
CONSTRAINT fk_eid FOREIGN KEY (eid)
REFERENCES emp1 (eid) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);//emp_bal-->employee balance leaves which is considered as 8
CREATE TABLE result
(
eid integer,
lid integer,
sd date,
ed date,
sida boolean,
ssida boolean,
rsno serial NOT NULL,
CONSTRAINT pk_rsno PRIMARY KEY (rsno)
);
插入的数据是
emp1
-----
eid | ename | sid | ssid
-----+-------+-----+------
1 | a | 2 | 8
3 | c | 4 | 9
2 | b | 3 | 8
4 | d | 2 | 8
5 | e | 2 | 8
6 | f | 4 | 9
(6 行)
leave_type
----------
eid | lid | lnum | emp_bal | sno
-----+-----+------+---------+-----
1 | 0 | 1 | 8 | 1
3 | 0 | 1 | 8 | 2
5 | 0 | 1 | 8 | 3
1 | 1 | 1 | 8 | 4
1 | 2 | 2 | 8 | 5
(5 rows)
result
-------
eid | lid | sd | ed | sida | ssida | rsno
-----+-----+------------+------------+------+-------+------
1 | 0 | 2013-01-01 | 2013-01-01 | t | f | 1
3 | 0 | 2013-01-09 | 2013-01-09 | t | f | 2
5 | 0 | 2013-01-11 | 2013-01-11 | t | f | 3
1 | 1 | 2013-02-14 | 2013-02-14 | t | f | 4
1 | 2 | 2013-03-15 | 2013-03-16 | f | t | 5
(5 rows)
询问 :
我想要更新审批表
CREATE TABLE approval
(
eid integer,
lid integer,
asid integer,
bal integer
);
作为输出
eid | lid | sid |bal
-----+-----+---+--
1 | 0 | 2 | 7
3 | 0 | 4 | 7
5 | 0 | 2 | 7
1 | 1 | 2 | 6
1 | 2 | 8 | 4
(5 rows)
条件:: 我尝试使用此查询将 sid 放入批准表,如下所述::
CASE WHEN r.sida='t'
THEN (update approval set a.asid=e.sid where a.eid=e.eid from emp1 e,approval a)
WHEN r.ssida='t'
THEN (update approval set a.asid=e.ssid where a.eid=e.eid from emp1 e,approval a)
ELSE 0
END
我希望平衡栏也应该根据 sid 验证进行更新,即,
CASE if r.sida='t' then bal=emp1.emp_bal-1 and emp_bal in emp1 should be updated to the latest value of bal from approval else if r.ssida='t' then bal=emp_bal-2 and emp_bal in emp1应从批准更新为 bal 的最新值
有没有办法解决这个问题?
最后,我想查看所有已批准的员工休假以及批准的人员以及剩余的休假!
完整的详细信息在 [SQL FIDDLE] ( http://sqlfiddle.com/#!12/3e6a7/18 )