oracle 在三个表中执行加法,并在其中一个表中更新它们的差值

zd287kbt  于 2023-06-22  发布在  Oracle
关注(0)|答案(2)|浏览(127)

需要在表中执行加法
表A

SELECT Unit, group, City, startdate, sum(Amt)
FROM gmr.BFR_CAL
Group By unit, group, city, startdate

sample data:-

tyre    mrf   hyder     12-04-23  49

Table - B

SELECT Unit, group, City, startdate, sum(Amt)
FROM gmr.DRAFT
WHERE ty = 6
Group By unit, group, city, startdate


sample data:-

tyre    mrf   hyder     12-04-23  5

Table - C

SELECT Unit, group, City, startdate, sum(Amt)
FROM gmr.DRAFT
WHERE ty <> 6
Group By unit, group, city, startdate;

sample data:-

tyre    mrf   hyder     12-04-23  18

需要查询
B+C!= A那么
更新类型为6的B(表)的AMT(列)
更新方程为

AMT= A-"B+C"
AMT=49-(18+5)
AMT=49-23
AMT= 26

因此,更新应该发生在表B中的col(Amt)中,其中先前的amt为5,但现在对于表B,而不是amt=5,现在应该是amt=26。

Finally Table B should look like :-

tyre    mrf   hyder     12-04-23  26
im9ewurl

im9ewurl1#

使用MERGE语句。你只是模糊地描述了你的表,以及你想要如何连接表和关联更新,但你似乎想要这样的东西:

MERGE INTO gmr.DRAFT dst
USING (
  SELECT a.unit,
         a.group_name,
         a.city,
         a.startdate,
         a.total,
         bc.total6,
         bc.totalNot6
  FROM   (
           SELECT Unit,
                  group_name,
                  City,
                  startdate,
                  sum(Amt) AS total
           FROM   gmr.BFR_CAL 
           GROUP BY
                  unit,
                  group_name,
                  city,
                  startdate
         ) a
         INNER JOIN (
           SELECT Unit,
                  group_name,
                  City,
                  startdate,
                  SUM(CASE WHEN ty =  6 THEN amt END) AS total6,
                  SUM(CASE WHEN ty <> 6 THEN amt END) AS totalNot6
           FROM   gmr.DRAFT
           Group By
                  unit,
                  group_name,
                  city,
                  startdate
         ) bc
         ON (   a.unit       = bc.unit
            AND a.group_name = bc.group_name
            AND a.city       = bc.city
            AND a.startdate  = bc.startdate )
) src
ON (   src.unit       = dst.unit
   AND src.group_name = dst.group_name
   AND src.city       = dst.city
   AND src.startdate  = dst.startdate )
WHEN MATCHED THEN
  UPDATE
  SET amt = (total + total6) - (total6 + totalNot6);

其中,对于样本数据:

CREATE TABLE gmr.DRAFT (unit, group_name, city, startdate, ty, amt) AS
SELECT 'unit1', 'grp1', 'city1', DATE '1970-01-01', 1, 100 FROM DUAL UNION ALL
SELECT 'unit1', 'grp1', 'city1', DATE '1970-01-01', 2, 200 FROM DUAL UNION ALL
SELECT 'unit1', 'grp1', 'city1', DATE '1970-01-01', 3, 300 FROM DUAL UNION ALL
SELECT 'unit1', 'grp1', 'city1', DATE '1970-01-01', 6, 400 FROM DUAL;

CREATE TABLE gmr.BFR_CAL (unit, group_name, city, startdate, amt) AS
SELECT 'unit1', 'grp1', 'city1', DATE '1970-01-01', 1000 FROM DUAL;

MERGE之后,然后:

SELECT * FROM gmr.DRAFT;

输出:
| 单位|集团名称|城市|开始日期|TY| AMT|
| - -----|- -----|- -----|- -----|- -----|- -----|
| 单位1| GRP1|城市1| 1970 - 01 - 01 00:00:00| 1|四百|
| 单位1| GRP1|城市1| 1970 - 01 - 01 00:00:00| 2|四百|
| 单位1| GRP1|城市1| 1970 - 01 - 01 00:00:00| 3|四百|
| 单位1| GRP1|城市1| 1970 - 01 - 01 00:00:00|六|四百|
并且这些值都已更新为新的总金额。
fiddle

xjreopfe

xjreopfe2#

使用缓冲表、临时表进行计算,然后可以添加行或更新。然后可以截断和删除缓冲区、临时表。

相关问题