来自不同表的Oracle总和

时间:2017-10-06 11:48:36

标签: sql oracle oracle12c

我想在PL / SQL Developer的Oracle DataBAse中对来自不同表的值进行求和,所以我准备了这个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') 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

1 个答案:

答案 0 :(得分:2)

您的查询有一些问题,我修复了这些问题,下面列出了从坏到坏的降序:

  • 你在子查询from pd_week_rec;中有一个迷路分号......这可能是你看到的特定错误的原因
  • 您在子查询之间使用UNION,如果两个子查询碰巧巧合的计数相同,则可能导致错误的结果
  • 您可能希望为派生表分配别名(某些SQL版本所需)
  • 您使用的是COUNT('1'),也许并没有错,但我会使用COUNT(*)代替


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;