我在sum group by statement上获得双倍价值

时间:2012-10-19 16:06:41

标签: sql oracle10g

Table
P_ID   I_ID   S_ID  T_ID Column  Column1



 -- (<Column1> supposed to be <Column>.<Table>-<Column>.<Table1>)


Table

P_ID   I_ID   S_ID  T_ID Column

我在源表上运行了这个查询:

select <column> from 
<TABLE>
where 
i_id='<value>';

我输出为9000 然后我在目标上运行此查询以测试id Column1正在获取正确的值

select P_ID, I_ID, S_ID, T_ID, 
sum(<column>)
from 
<TABLE> F,<TABLE2> PO
where 
F.P_ID = PO.P_ID
and F.I_ID=PO.I_ID
and F.FLAG='Y'  --something that we need
and T_id>='2012001'  --just for results for 2012
and F.I_id='<Value>'
group by F.P_ID, F.I_ID, F.S_ID, F.T_ID
order by 1;



Here i get Output as
  P_ID     I_ID           S_ID          T_ID           sum(<column>)
 <Value>  <Value>         <Value>      <Value>              18000    

1 个答案:

答案 0 :(得分:1)

显然,PO中的每一行F都有多行。

此外,将您的查询更改为更现代(自1996年以来!)的连接语法:

from <TABLE> F
join <TABLE2> PO ON F.P_ID = PO.P_ID and F.I_ID=PO.I_ID
where F.FLAG='Y'
...

编辑:

如果您只想声明 PO中的一行(不关心有多少行),请使用exists()

select P_ID, I_ID, S_ID, T_ID, 
sum(<column>)
from <TABLE> F
where exists (
    select * from <TABLE2> PO 
    where F.P_ID = PO.P_ID
    and F.I_ID=PO.I_ID)
and F.FLAG='Y'  --something that we need
and T_id>='2012001'  --just for results for 2012
and F.I_id='<Value>'
group by F.P_ID, F.I_ID, F.S_ID, F.T_ID
order by 1;