Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用大小写和条件更新表(postgresql)_Sql_Postgresql_Postgresql 9.2 - Fatal编程技术网

使用大小写和条件更新表(postgresql)

使用大小写和条件更新表(postgresql),sql,postgresql,postgresql-9.2,Sql,Postgresql,Postgresql 9.2,我有桌子:: 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, sn

我有桌子::

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排)

查询:

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)
);
我想更新审批表

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验证进行更新,即

如果r.sida='t',则bal=emp1.emp_bal-1和emp1中的emp_bal应 如果r.ssida='t' 然后bal=emp_bal-2,emp1中的emp_bal应更新为最新版本 来自批准的余额值

有办法解决这个问题吗

最后,我想查看所有已批准的员工假期以及批准者,还有剩余的假期

详细信息请参见[SQL FIDLE]()

您做错了

在Update语句中使用Case语句,而不是在Case语句中使用Update语句


在你澄清之后,我删除了我的答案并投了反对票。你的更新很有意义。其他人可能会对此进行调查。谢谢你…@craig ringeri没有得到你@parth Malhan你能在更新语句中处理此案例语句吗
Update **Table**
Set **Col1**= 

Case when **Col10=1** then 5

else case when **Col10=2** THEN 6

**ELSE 10** END  
**ELSE 15** END