請問大大為什麼
select sum(a),sum(b),sum(c)
form (select a,b,c from table_a where kk > 12 )
與
select *
form (select sum(a),sum(b),sum(c) from table_a where kk > 12)
得到的值不一樣 ?
兩個要一樣才對,有資料佐證?
以下是測試demo
with table_a as (
select 1 a ,1 b ,1 c, 11 kk from dual union all
select 1 a ,1 b ,1 c, 12 kk from dual union all
select 1 a ,1 b ,null c, 13 kk from dual union all
select 1 a ,1 b ,1 c, 14 kk from dual
)
select sum(a),sum(b),sum(c)
from (select a,b,c from table_a where kk > 12 );
-- Result : 2,2,1
with table_a as (
select 1 a ,1 b ,1 c, 11 kk from dual union all
select 1 a ,1 b ,1 c, 12 kk from dual union all
select 1 a ,1 b ,null c, 13 kk from dual union all
select 1 a ,1 b ,1 c, 14 kk from dual
)
select *
from (select sum(a),sum(b),sum(c) from table_a where kk > 12)
-- Result : 2,2,1
select *
form (select sum(a),sum(b),sum(c) from table_a where kk > 12)
這個會錯吧?? subQuery 沒給別名, 感覺語法錯誤..
我會建議改成這樣
select DATA.*
form (
select sum(a) SUMa, sum(b) SUMb, sum(c) SUMc
from table_a
where kk > 12) DATA