1
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    
4

1 回答 1

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;
于 2012-10-19T16:15:18.350 回答