0

テーブルの列を別のテーブルのデータで更新しようとしましたdeletiondateが、「式がありません」というエラーが表示されます。誰かがこの問題を解決するのを手伝ってくれますか?

deletiondate基本的に、キーフィールドを別のテーブルと結合してグループ化することで、テーブルの列を更新したいと考えています。日付が01-JAN-00011 より大きく、レコード数が 101-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 に答える