oracle 要在SQL中插入数据一个表与另一个表的条件组合

0dxa2lsx  于 2023-08-04  发布在  Oracle
关注(0)|答案(3)|浏览(173)

如何插入数据到一个表相比,另一个表wrt一些条件?
两个表的结构相同。

insert into x.Summing (a, b, c, d, e, f) 
values 
    (select a, b, c, d, e, sum(f) 
     from
         ((select a, b, c, d, e, f from x.draft 
           where d = 1 
             and substr(A, -2, 2) in (select ODE from x.ram
                                      where city = 'KT'))
          union all
          (select a, b, c, d, e, sum(f) from x.draft 
           where d != 1 
             and substr(A, -2, 2) in (select ODE from x.ram
                                      where city = 'KT'))
    )
group by a, b, c, d, e;

字符串
但我得到了这个错误:
SQL错误:ORA-00936:缺失表达式
00936. 00000 -“缺少表达式”

4ngedf3f

4ngedf3f1#

您发布的查询无效; values应该被删除(因为你没有插入单独的值,而是select语句的结果)。
此外,查询本身无效。如果没有示例数据和虚拟列名,很难猜测您在做什么,但是第二个union查询还需要group by子句,因为它对f求和,所以所有其他非聚合列都必须放入group by中。
重写后,就语法而言,这个 * 应该 *(不能测试它)是可以的。

INSERT INTO x.Summing (a,
                       b,
                       c,
                       d,
                       e,
                       f)
     SELECT a,
            b,
            c,
            d,
            e,
            SUM (f)
       FROM (
               (SELECT a,
                       b,
                       c,
                       d,
                       e,
                       f
                  FROM x.draft
                 WHERE     d = 1
                       AND SUBSTR (A, -2, 2) IN (SELECT ODE
                                                   FROM x.ram
                                                  WHERE city = 'KT'))
               UNION ALL
               (  SELECT a,
                         b,
                         c,
                         d,
                         e,
                         SUM (f)
                    FROM x.draft
                   WHERE     d != 1
                         AND SUBSTR (A, -2, 2) IN (SELECT ODE
                                                     FROM x.ram
                                                    WHERE city = 'KT')
                GROUP BY a,
                         b,
                         c,
                         d,
                         e)
            )
   GROUP BY a,
            b,
            c,
            d,
            e;

字符串

9gm1akwq

9gm1akwq2#

您错过了命令是INSERT INTO SELECT
所以你可以

insert into x.Summing (a,b,c,d,e,f)  
 select  a,b,c,d,e,sum(f) 
 from
    (
        (Select  a,b,c,d,e,f from x.draft 
        where  d=1 and SUBSTR(A,-2,2) IN (SELECT ODE FROM x.ram
                                                 WHERE city='KT'))
    union all
    (Select  a,b,c,d,e,sum(f) from x.draft 
    where  d!=1 and SUBSTR(A,-2,2) IN (SELECT ODE FROM x.ram
                                                 WHERE city='KT'))
)
group by a,b,c,d,e;

字符串

z18hc3ub

z18hc3ub3#

这两个子查询之间的唯一区别是d = 1d != 1上的过滤器。您可以将这两个条件组合成d = 1 OR d != 1,然后进一步组合成d IS NOT NULL。一旦你这样做了,那么UNION就不必要了:

insert into x.Summing (a,b,c,d,e,f)
  select  a,b,c,d,e,sum(f)
  from    x.draft 
  where   d IS NOT NULL
  and     SUBSTR(A,-2,2) IN (SELECT ODE
                             FROM   x.ram
                             WHERE  city='KT')
  group by a,b,c,d,e;

字符串
除此之外,VALUES关键字有一个语法错误(在使用INSERT INTO ... SELECT ...时不需要)。

相关问题