Sql 来自不同表的Oracle总和
我想在PL/SQL开发人员提供的Oracle数据库中对不同表中的值求和,如下所示,因此我准备了以下SQL语句:Sql 来自不同表的Oracle总和,sql,oracle,oracle12c,Sql,Oracle,Oracle12c,我想在PL/SQL开发人员提供的Oracle数据库中对不同表中的值求和,如下所示,因此我准备了以下SQL语句: select sum(total) as ttt from (select count('1') as total from vehicle_hotel union select count('1') as total from alarm union select count('1') as total from vd_poi union select count('1') a
select sum(total) as ttt from
(select count('1') as total
from vehicle_hotel
union
select count('1') as total
from alarm
union
select count('1') as total
from vd_poi
union
select count('1') as total
from person_hotel
union
select count('1') as total
from social_office_transaction
union
select count('1') as total
from person_hotel_field_value
union
select count('1') as total
from pd_trf_week
union
select count('1') as total
from aggreg_exception
union
select count('1') as total
from pd_week_rec;
select count('1') as total
from hist_pd_week_rec
union
select count('1') as total
from pd_week);
但我有一个错误:
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Error en la línea: 32, columna: 12
您的查询有一些问题,我已经解决了,下面按从坏到不坏的降序排列: 在pd_week_rec;的子查询中有一个分号。。。这可能是您看到的特定错误的原因 您在子查询之间使用UNION,如果两个子查询碰巧具有相同的计数,则可能会导致不正确的结果 您可能希望为特定版本的SQL所需的派生表分配一个别名 您使用了COUNT'1',也许没有错,但我会使用COUNT*
您的查询有一些问题,我已经解决了,下面按从坏到不坏的降序排列: 在pd_week_rec;的子查询中有一个分号。。。这可能是您看到的特定错误的原因 您在子查询之间使用UNION,如果两个子查询碰巧具有相同的计数,则可能会导致不正确的结果 您可能希望为特定版本的SQL所需的派生表分配一个别名 您使用了COUNT'1',也许没有错,但我会使用COUNT*
Oracle不需要子查询的别名。问题是分号。你还应该解释为什么union all是正确的,union不一定会返回正确的结果。我不知道这一点。select*from select 1记录from dual实际返回一条记录。直到今天,我还以为它会崩溃。@DanBracuk我也不是\:~@MT0我说的也许没有错,但我从未在生产代码库中看到过计数“1”。Oracle不需要子查询的别名。问题是分号。你还应该解释为什么union all是正确的,union不一定会返回正确的结果。我不知道这一点。select*from select 1记录from dual实际返回一条记录。直到今天,我还以为它会崩溃。@DanBracuk我也没有\:~@MT0我说的也许没有错,但我从未在生产代码库中看到计数“1”。使用UNION ALL not UNION-如果两个表中的计数相同,则UNION将删除重复项并导致计数不足。使用UNION ALL not UNION-如果两个表中的计数相同,则UNION将删除重复项并导致计数不足。
select sum(total) as ttt
from
(
select count(*) as total
from vehicle_hotel
union all
select count(*)
from alarm
union all
select count(*)
from vd_poi
union all
select count(*)
from person_hotel
union all
select count(*)
from social_office_transaction
union all
select count(*)
from person_hotel_field_value
union all
select count(*)
from pd_trf_week
union all
select count(*)
from aggreg_exception
union all
select count(*)
from pd_week_rec
select count(*)
from hist_pd_week_rec
union all
select count(*)
from pd_week
) t;