我尝试使用另一个表中的数据更新表列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
答案 0 :(得分: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 |