0

我尝试deletiondate使用另一个表中的数据更新表列,但出现“缺少表达式”错误。有人可以帮我解决这个问题吗?

基本上我想deletiondate通过将关键字段与另一个表连接并进行分组来更新表中的列。如果日期是01-JAN-0001并且记录数 > 1,则需要更新01-JAN-0001,否则我需要更新最大删除日期值。

我使用的更新声明:

update table1 db1 set deletiondate =

SELECT  
CASE WHEN count(*)>1 and ( 
    (select 1 
     from table2 b 
     where b.loginid = a.loginid
         and a.creationdate = b.creationdate 
         and b.deletiondate = '01-JAN-0001'
     ) = 1) THEN '01-JAN-0001' ELSE to_char(MAX(deletiondate),'DD-MON-YYYY') END as deletiondate1  
FROM table2 a 
GROUP BY a.loginid, a.creationdate 
WHERE db1.userloginid = a.loginid and db1.usercreationdate = a.creationdate 
4

1 回答 1

1

使用这种格式:http ://www.sqlfiddle.com/#!4/c46a6/2

update product set
  (total_qty,max_qty) = 
(
  select sum(qty) as tot, max(qty) as maxq
  from inv
  where product_id = product.product_id
  group by product_id
) ;

样本数据:

create table product(
  product_id int primary key,
  product_name varchar(100),
  total_qty int,
  max_qty int
);

create table inv(
  inv_id int primary key,
  product_id int references product(product_id),
  qty int
);


insert into product(product_id,product_name) 
select 1,'KB' from dual
union
select 2, 'MSE' from dual
union
select 3, 'CPU' from dual;

insert into inv(inv_id,product_id,qty)
select 1,1,4 from dual
union
select 2,2,1 from dual
union
select 3,3, 3 from dual
union
select 4,1,1 from dual
union
select 5,2,2 from dual
union
select 6,1,5 from dual;

查询输出:

| PRODUCT_ID | PRODUCT_NAME | TOTAL_QTY | MAX_QTY |
|------------|--------------|-----------|---------|
|          1 |           KB |        10 |       5 |
|          2 |          MSE |         3 |       2 |
|          3 |          CPU |         3 |       3 |
于 2012-05-24T23:37:29.217 回答